php 实例化派生类时,是否不隐式调用抽象类构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2321009/
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
Are abstract class constructors not implicitly called when a derived class is instantiated?
提问by scottm
Take this example:
拿这个例子:
abstract class Base {
function __construct() {
echo 'Base __construct<br/>';
}
}
class Child extends Base {
function __construct() {
echo 'Child __construct<br/>';
}
}
$c = new Child();
Coming from a C# background, I expect the output to be
来自 C# 背景,我希望输出是
Base __construct
Child __construct
基础 __construct
子 __construct
However, the actual output is just
然而,实际输出只是
Child __construct
子 __construct
回答by Pascal MARTIN
No, the constructor of the parent class is not called if the child class defines a constructor.
不,如果子类定义了构造函数,则不会调用父类的构造函数。
From the constructor of your child class, you have to call the constructor of the parent's class :
从子类的构造函数中,您必须调用父类的构造函数:
parent::__construct();
Passing it parameters, if needed.
如果需要,传递它的参数。
Generally, you'll do so at the beginning of the constructor of the child class, before any specific code ; which means, in your case, you'd have :
通常,您将在子类的构造函数的开头、任何特定代码之前执行此操作;这意味着,在您的情况下,您将拥有:
class Child extends Base {
function __construct() {
parent::__construct();
echo 'Child __construct<br/>';
}
}
And, for reference, you can take a look at this page of the PHP manual : Constructors and Destructors-- it states (quoting):
而且,作为参考,您可以查看 PHP 手册的这一页:构造函数和析构函数——它指出(引用):
Note:Parent constructors are not called implicitly if the child class defines a constructor.
In order to run a parent constructor, a call toparent::__construct()within the child constructor is required.
注意:如果子类定义了构造函数,则不会隐式调用父构造函数。
为了运行父构造函数,需要parent::__construct()在子构造函数中调用 。
回答by scottm
Well, I just found this in the docs:
好吧,我刚刚在文档中找到了这个:
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
注意:如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用 parent::__construct()。
回答by Otamay
If you need the same behaviour as C#, that is the parent constructor gets always executed before child constructor, you could create a fake constructor class for your child classes and declare it as an abstract function in your abstract parent class.
如果您需要与 C# 相同的行为,即父构造函数总是在子构造函数之前执行,您可以为子类创建一个伪构造函数类,并将其声明为抽象父类中的抽象函数。
E.g.
例如
abstract class Test{
abstract public function __childconstruct();
public function __construct(){
echo "SOME CODE".PHP_EOL;
$this->__childconstruct();
}
}
class TestExtended extends Test{
public function __childconstruct(){
echo "SOME OTHER CODE FROM EXTENDED CLASS".PHP_EOL;
}
}
$a = new TestExtended();
/* SOME CODE
SOME OTHER CODE FROM EXTENDED CLASS */

