php php中调用父方法的多种方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11237511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:02:42  来源:igfitidea点击:

multiple ways of calling parent method in php

php

提问by jerry

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 parentor 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.

现在我不知道是否有一个首选的方式调用方法(通过(即最佳实践)parentthis),这和是否是在执行相同的代码的真正相同的方式,或使用一个以上的时候,如果有什么注意事项另一个。

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;

回答by Anthony

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 $thisyou stay consistent between inherited methods and locally declared methods.

  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.

  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:

    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.";
         }
    }
    
  1. 方法不会被子类覆盖,只存在于父类中。

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

  2. 方法被子类覆盖,并且与父类具有完全独特的逻辑。

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

  3. 方法扩展了父类,增加了父方法实现的功能。

    在这种情况下,您仍然希望`$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.

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

回答by hackartist

Unless I am misunderstanding the question, I would almost always use $this->get_species because the subclass (in this case dog) could overwrite that method since it does extend it. If the class dog doesn't redefine the method then both ways are functionally equivalent but if at some point in the future you decide you want the get_species method in dog should print "dog" then you would have to go back through all the code and change it.

除非我误解了这个问题,否则我几乎总是使用 $this->get_species 因为子类(在这种情况下为狗)可以覆盖该方法,因为它确实扩展了它。如果类 dog 没有重新定义该方法,那么两种方式在功能上都是等效的,但是如果在将来的某个时候您决定希望 dog 中的 get_species 方法应该打印“dog”,那么您将不得不返回所有代码并更改。

When you use $this it is actually part of the object which you created and so will always be the most up-to-date as well (if the property being used has changed somehow in the lifetime of the object) whereas using the parent class is calling the static class method.

当您使用 $this 时,它实际上是您创建的对象的一部分,因此也将始终是最新的(如果正在使用的属性在对象的生命周期中以某种方式发生了变化),而使用父类正在调用静态类方法。