如何在父类中自动运行构造而不从子类调用
我正在寻找一种从子类自动调用父类构造函数(?)的方法:
(注意:这只是一个示例,因此可能会出现输入错误)
Class myParent()
{
protected $html;
function __construct( $args )
{
$this->html = $this->set_html( $args );
}
protected function set_html( $args )
{
if ( $args['foo'] === 'bar' )
$args['foo'] = 'foobar';
return $args;
}
}
Class myChild extends myParent
{
public function do_stuff( $args )
{
return $this->html;
}
}
Class myInit
{
public function __construct( $args )
{
$this->get_stuff( $args );
}
public function get_stuff( $args )
{
$my_child = new myChild();
print_r( $my_child->do_stuff( $args ) );
}
}
$args = array( 'foo' => 'bar, 'what' => 'ever' );
new myInit( $args );
// Should Output:
/* Array( 'foo' => 'foobar', 'what' => 'ever' ) */
我想要什么要避免的是必须调用(在类 myChild 内) __construct( $args ) {parent::__construct( $args ); }
。
问题:这可能吗?如果是这样:如何?
谢谢!
I'm searching for a way to call a parent class constructor(?) auto-magically from a child class:
(Note: This is just an example, so typing errors may be present)
Class myParent()
{
protected $html;
function __construct( $args )
{
$this->html = $this->set_html( $args );
}
protected function set_html( $args )
{
if ( $args['foo'] === 'bar' )
$args['foo'] = 'foobar';
return $args;
}
}
Class myChild extends myParent
{
public function do_stuff( $args )
{
return $this->html;
}
}
Class myInit
{
public function __construct( $args )
{
$this->get_stuff( $args );
}
public function get_stuff( $args )
{
$my_child = new myChild();
print_r( $my_child->do_stuff( $args ) );
}
}
$args = array( 'foo' => 'bar, 'what' => 'ever' );
new myInit( $args );
// Should Output:
/* Array( 'foo' => 'foobar', 'what' => 'ever' ) */
What I want to avoid is having to call (inside Class myChild) __construct( $args ) { parent::__construct( $args ); }
.
Question: Is this possible? If so: How?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的示例代码中,myParent::__construct 在实例化 myChild 时已经被调用。
要让您的代码按照您想要的方式工作,只需更改
只要 myChild 没有构造函数,父构造函数将被调用/继承。
In your sample code, myParent::__construct will already get called wen instanciating myChild.
To get your code to work as you want simply change
by
As long as myChild has no constructor, the parent constructor will be called / inherited.
由于
Child
没有构造函数,并且扩展Parent
,因此只要指定new Child()
,父构造函数将被隐式调用。
如果您确实指定了
Child
构造函数,那么您必须在Child
构造函数中使用指定parent::__construct();
,因为它不会隐式调用。注意在子类中定义构造函数时,最佳做法是在方法定义的第一行调用
parent::__construct()
,以便继承任何实例参数和状态在子类启动之前设置。As
Child
has no constructor present and extendsParent
, any timenew Child()
is specified theParent
constructor will be implicitly called.If you do specify a
Child
constructor then you have to use specifyparent::__construct();
inside theChild
constructor as it will not be called implicitly.N.B When defining a constructor in a subclass it is best practice to call
parent::__construct()
on the first line of the method definition so that any instance parameters and state inherited is set prior to subclass initiation.