PHP继承
我有以下 3 个类:
class ParentClass {
protected $myVar = array();
function __construct() {
var_dump($this->myVar);
}
}
class FirstClass extends ParentClass {
protected $myVar = array('defaultVal');
}
class SecondClass extends FirstClass {
protected $myVar = array('anotherVal');
}
如果我要执行以下操作:
$class = new SecondClass();
那么我会得到 array('anotherVal')
我可以将什么添加到 ParentClass 的构造中,以便我实际上得到 array('defaultVal', 'anotherVal')
提前致谢!
I have the following 3 classes :
class ParentClass {
protected $myVar = array();
function __construct() {
var_dump($this->myVar);
}
}
class FirstClass extends ParentClass {
protected $myVar = array('defaultVal');
}
class SecondClass extends FirstClass {
protected $myVar = array('anotherVal');
}
If I was to do the following:
$class = new SecondClass();
Then I would get array('anotherVal')
what can I add to the construct of ParentClass so that I would actually get array('defaultVal', 'anotherVal')
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此页面中的想法,我建议如下
:以下输出:
Using ideas from this page, I would suggest the following:
Which gives me the following output:
经过进一步研究,我找到了一种简单的方法get_class_vars :
这解决了我的问题...
无论如何谢谢大家!
After furthur research i found out an easy way to do it get_class_vars:
This solves my problems...
Thanks anyway guys!
出于某种奇怪的原因,堆栈溢出不断删除我之前的答案。这是新的。
For some odd reason, stack overflow keeps erasing my previous answer. So here is the new one.
将您的分配更改为 array_push()。
Change your assignments to array_push().