PHP - 扩展 __construct
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3477224/
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
PHP - extended __construct
提问by Lee
I was wondering if you could help me out..
我想知道你是否可以帮助我..
I have two classes, one extends the other.. Class B will be extended by various different objects and used for common database interactions.. Now I would like class B to handle its connect and disconnects without direction from class A or any external input..
我有两个类,一个扩展另一个.. B 类将被各种不同的对象扩展并用于常见的数据库交互.. 现在我希望 B 类在没有来自 A 类或任何外部输入的指示的情况下处理它的连接和断开连接。 .
The problem from what I understand is that an extended class won't automatically run its __construct function.. Is there a way around this?
据我所知,问题是扩展类不会自动运行其 __construct 函数。有没有办法解决这个问题?
Thanks in advance..
提前致谢..
class a extends b
{
public function __construct()
{
}
public function validateStuff()
{
$this->insert_record();
}
}
class b
{
public function __construct()
{
$this->connect();
}
protected function connect()
{
return true;
}
public function insert_record()
{
return true;
}
}
回答by Mark Baker
The parent __construct()
method defined in class b will run automatically if you instantiate child class a, unlessthere is a __construct()
method defined in class a.
__construct()
如果您实例化子类 a,则在类 b 中定义的父方法将自动运行,除非__construct()
在类 a 中定义了一个方法。
class a extends b {
}
class b {
public function __construct()
{
echo 'In B Constructor';
}
}
$x = new a();
If a __construct()
method is defined in class a, then this overrides the use of the __construct()
method in class b.... it will run instead ofthe class b __construct()
method
如果__construct()
在类 a 中定义了一个方法,那么这将覆盖__construct()
类 b 中方法的使用......它将运行而不是类 b 的__construct()
方法
class a extends b {
public function __construct()
{
echo 'In A Constructor';
}
}
class b {
public function __construct()
{
echo 'In B Constructor';
}
}
$x = new a();
So if your child class has a __construct()
method defined, then you need to explicitly call the constructor for the parent if you want to execute that as well.
因此,如果您的子类__construct()
定义了一个方法,那么如果您也想执行它,则需要显式调用父类的构造函数。
class a extends b {
public function __construct()
{
parent::__construct();
echo 'In A Constructor';
}
}
class b {
public function __construct()
{
echo 'In B Constructor';
}
}
$x = new a();
回答by GWW
I'm not sure I fully understand what you are asking, but you can call the parents construct method from the child's constructor
我不确定我是否完全理解你在问什么,但你可以从孩子的构造函数中调用父母构造方法
parent::__construct();
That's the only option I know of.
这是我所知道的唯一选择。
回答by Annika Backstrom
Call parent::__construct()
in a::__construct()
:
通话parent::__construct()
中a::__construct()
:
class a extends b
{
public function __construct()
{
parent::__construct();
}
public function validateStuff()
{
$this->insert_record();
}
}
You can omit a's constructor altogether if you're not doing any a-specific stuff.
如果您不做任何特定于 a 的事情,您可以完全省略 a 的构造函数。