php中调用父方法的多种方法 [英] multiple ways of calling parent method in php

查看:173
本文介绍了php中调用父方法的多种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初,我很困惑为什么构造函数中的两个方法都可以工作,但是现在我想我明白了.扩展类继承父类的方法,就像它们是在类本身中声明的一样,并且这些方法都存在于父类中,因此两者都应该起作用.

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.

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

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;

推荐答案

在三种情况下(我可以想到),您将在子类中调用方法,而该方法在父类中退出:

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. 方法不会被子类覆盖,仅存在于父类中.

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

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

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.

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

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

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

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.

方法扩展了父类,并添加了父方法所实现的功能.

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

在这种情况下,从子类的其他方法调用该方法时,您仍然要使用`$this -> get_species();.您将要调用父方法的一个地方是覆盖父方法的方法.示例:

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.";
     }
}

我能想象的唯一场景是,您需要在覆盖方法之外直接调用父方法的情况是,如果他们做了两件事,而您知道您需要方法的父版本,而不是本地版本.情况并非如此,但是如果它确实能够实现,那么解决此问题的干净方法将是创建一个名称类似于get_parentSpecies()的新方法,其中所有操作都称为父方法:

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:

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.

这篇关于php中调用父方法的多种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆