简体   繁体   English

如何在PHP中的子构造函数中访问父级方法?

[英]How do I access a parent's methods inside a child's constructor in PHP?

Say I have class child() and class parent() . 假设我有class child()class parent() The parent has a constructor and a few other public methods, and the child is empty apart from a constructor. 父有一个构造函数和一些其他公共方法,除了构造函数之外,子对象是空的。

How do I go about calling a parent's methods inside of the child's constructor, as in: 如何在子元素的构造函数中调用父元素的方法,如:

Class Parent {
    public function __construct() {
       // Do stuff (set up a db connection, for example)
    }

    public function run($someArgument) {
       // Manipulation
       return $modifiedArgument;
    }
} 

Class Child extends Parent {
    public function __construct() {
      // Access parent methods here?
    }  
}

Say I want to call parent s run() method, do I have to call a new instance of the parent inside the child constructor? 假设我想调用parent s的run()方法,我是否必须在子构造函数中调用父实例的新实例? Like so... 像这样......

$var = new Parent(); 
$var->run($someArgument);

If so, what is the point of extends from a class definition POV? 如果是这样,那么从类定义POV extends到什么意义呢? I can call a new instance of another class with the new keyword whether it extends the 'child' or not. 我可以用new关键字调用另一个类的新实例,无论它是否扩展了'child'。

My (likely) wrong understanding was that by using extends you can link classes and methods from a parent can be inherited into the child. 我(可能)错误的理解是,通过使用extends您可以将来自父级的类和方法链接到子级中。 Is that only outside the class definition? 这只是在课程定义之外吗? Does using extend offer no efficiencies inside the class definition? 使用extend是否在类定义中没有提供效率?

Because referring to the parent's run() method with the this keyword certainly doesn't work... 因为使用this关键字引用父类的run()方法肯定不起作用...

Use parent as predefined reference: parent::run() . 使用parent作为预定义引用: parent::run() This will ensure you call parent method. 这将确保您调用父方法。 The same way you could call first parent constructor first or after child one - parent::__construct() . 您可以先调用第一个父构造函数,也可以在子类1之后调用相同的方法 - parent::__construct()

Class Child extends Parent {
    public function __construct() {
      parent::__construct();
      // Access parent methods here?
      $some_arg = NULL; // init from constructor argument or somewhere else
      parent::run($some_arg); // explicitly call parent method
      // $this->run($some_arg); // implicitly will call parent if no child override
    }  

} }

If you dont have an implementation in child you could call $this->run($args) , where it will again call parent run method. 如果你没有在子项中实现,你可以调用$this->run($args) ,它将再次调用parent run方法。

To extend Rolice's answer 扩展Rolice的答案

function a() {
  echo 'I exist everywhere';
}
class A {
  protected $a
  function a() {
    $this->a = 'I have been called';
  }
  function out() {
    echo $this->a;
    a();
  }
}
class B extends A {
  function __construct() {
    parent::a();// original method
    $this->a(); // overridden method
    a();
  }
  function a() {
    $this->a = $this->a ? 'I have been overwritten' : 'first call';
  }
}

Study these to understand the difference 研究这些以了解其中的差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM