有没有办法在没有构造函数的情况下将成员变量分配给类的对象?
问题描述:
I want to do something like the following:
class SomeOtherClass {}
class Test
{
public $member = new SomeOtherClass();
}
The only problem is that I do not want to use a constructor, because the 'Test' class should extend another class and should not override the constructor. Is this actually possible in PHP?
我想做类似以下的事情: p>
class SomeOtherClass {}
class Test
{
public $ member = new SomeOtherClass();
}
code> pre>
唯一的问题是我没有 想要使用构造函数,因为'Test'类应该扩展另一个类,不应该覆盖构造函数。
这实际上可以在PHP中使用吗? p>
div>
答
You can extend parent constructor in Test
like this:
class Test extends SomeClass
{
public $member;
function __construct() {
$this->member = new SomeOtherClass();
parent::__construct();
}
}