At first I was confused why both of the method calls in the constructor work, but now I think I understand. The extending classes inherit the parent's methods as if they were declared in the class itself, AND the methods exist in the parent, so both should work.

一开始我很困惑为什么构造函数中的两个方法调用都能工作,但是现在我想我理解了。扩展类继承父类的方法,就好像它们是在类本身中声明的一样,并且这些方法存在于父类中,所以这两个方法都应该有效。

Now I'm wondering if there is a preferred way (i.e. best practice) of calling the method (via parent or this), and whether or not these are truly identical ways of executing the same code, or if there are any caveats when using one over the other.

现在我想知道是否有一种首选的方法(即最佳实践)调用该方法(通过父方法或此方法),以及这些方法是否真正相同地执行相同的代码,或者在使用该方法时是否有任何注意事项。

Sorry, I'm probably over thinking this.

对不起,我可能已经忘了这个了。

abstract class Animal {

    function get_species() {

        echo "test";

    }

}

class Dog extends Animal {

    function __construct(){

        $this->get_species();
        parent::get_species();

    }

}

$spike = new Dog;

2 个解决方案

#1


82

There are three scenarios (that I can think of) where you would call a method in a subclass where the method exits in the parent class:

有三种情况(我可以想到)在子类中调用方法,其中方法在父类中退出:

  1. Method is not overwritten by subclass, only exists in parent.

    方法不会被子类覆盖,只存在于父类中。

    This is the same as your example, and generally it's better to use $this -> get_species(); You are right that in this case the two are effectively the same, but the method has been inherited by the subclass, so there is no reason to differentiate. By using $this you stay consistent between inherited methods and locally declared methods.

    这与您的示例相同,通常最好使用$ This -> get_species();您是对的,在这种情况下,这两者实际上是相同的,但是方法已经被子类继承,所以没有理由进行区分。通过使用$this,您可以在继承方法和本地声明方法之间保持一致。

  2. Method is overwritten by the subclass and has totally unique logic from the parent.

    方法由子类覆盖,父类具有完全惟一的逻辑。

    In this case, you would obviously want to use $this -> get_species(); because you don't want the parent's version of the method executed. Again, by consistently using $this, you don't need to worry about the distinction between this case and the first.

    在本例中,显然需要使用$this -> get_species();因为您不希望执行父类的方法版本。同样,通过始终使用$this,您不需要担心这种情况和第一种情况之间的区别。

  3. Method extends parent class, adding on to what the parent method achieves.

    方法扩展父类,添加父方法实现的内容。

    In this case, you still want to use `$this -> get_species(); when calling the method from other methods of the subclass. The one place you will call the parent method would be from the method that is overwriting the parent method. Example:

    在本例中,仍然需要使用' $this -> get_species();当从子类的其他方法调用该方法时。您将调用父方法的地方是覆盖父方法的方法。例子:

    abstract class Animal {
    
        function get_species() {
    
            echo "I am an animal.";
    
        }
    
     }
    
     class Dog extends Animal {
    
         function __construct(){
    
             $this->get_species();
         }
    
         function get_species(){
    
             parent::get_species();
             echo "More specifically, I am a dog.";
         }
    }
    

The only scenario I can imagine where you would need to call the parent method directly outside of the overriding method would be if they did two different things and you knew you needed the parent's version of the method, not the local. This shouldn't be the case, but if it did present itself, the clean way to approach this would be to create a new method with a name like get_parentSpecies() where all it does is call the parent method:

我能想象的唯一场景是,您需要在重写方法之外直接调用父方法,如果它们做了两件不同的事情,并且您知道您需要的是父方法的版本,而不是本地方法。这种情况不应该出现,但如果它确实出现了,那么实现这一点的干净方法将是创建一个名为get_parentSpecies()的新方法,它所做的就是调用父方法:

function get_parentSpecies(){

     parent::get_species();
}

Again, this keeps everything nice and consistent, allowing for changes/modifications to the local method rather than relying on the parent method.

同样,这使一切都保持良好和一致,允许对本地方法进行更改/修改,而不是依赖于父方法。

更多相关文章

  1. PHP + MySQL 实现无限分类的2种方法
  2. PHP 站点相对包含,路径的问题解决方法(include,require)
  3. 您是否认为PHP中的错误形式是在类方法中访问超级全局变量?
  4. 一个简易的PHP读取CSV文件的方法
  5. 用PHP计算字符串中元音的简单方法?
  6. php项目防止sql注入的方法
  7. PHP 创建对象的两种方法
  8. 仅获取单元素php数组中的值的最佳方法。
  9. 在CodeIgniter中包含视图的最佳方法。

随机推荐

  1. Android(安卓)JNI入门第三篇――jni头文
  2. Android软键盘-弹起时布局向上拉-登录界
  3. Error running app: Instant Run require
  4. Android中Activity的4种加载模式
  5. android中的震动
  6. android asset中 zip包解压sdcard
  7. Android(安卓)线程 Handler详解
  8. Android全屏Activity的几种方式
  9. Android getResources的作用和需要注意点
  10. 15个Android很有用的代码片段