PHP继承问题,吓死我了
有一个问题让我很困惑。父构造函数是否会在不被调用的情况下构造自身?
因为我看到它在某种情况下起作用,而不是在我写的剧本上。
我得到以下内容:
类路由器上的boot.php
$this->router = new router($param);
router.php
class router extends routerController
- 我没有任何 __construct。
routercontroller.php
class routerController{
function __construct($param){
$this->param = $param;
}
- 如果我向类路由器添加 __construct ,则不会执行扩展类 routerController 中的 __construct 。因此,在这种情况下,构造函数似乎是在没有parent::_construct 的情况下构造的。
在同一个脚本中,我进入了 routerController 类。
$this->newclass = newclass($this->param);
newClass 扩展了 someClass。由于某种原因,如果我不使用parent::__construct($param) 从 newClass 调用 someClass 构造函数,则该构造函数不会被实例化。
我已经花了几个小时检查所有代码,但找不到我做错了什么。为什么在第一种情况下,父构造函数在没有被调用的情况下被实例化,而在第二种情况下却没有被实例化?
这是一个错误吗?知道我做错了什么吗?
Got a question that is freaking me out. Do parent constructors constructs itself without being called?
Cause Im seeing it working on a situation and not on the script Im writing.
I got the following:
boot.php
$this->router = new router($param);
router.php
class router extends routerController
- on the class router I don't have any __construct.
routercontroller.php
class routerController{
function __construct($param){
$this->param = $param;
}
- if I add a __construct to the class router the __construct from the extended class routerController is not executed. So, it seems on this case the constructor is being constructed without a parent::_construct.
On the same script I got inside the routerController class.
$this->newclass = newclass($this->param);
newClass extends someClass. For some reason the someClass constructor is not instantiated if I don't call it from the newClass using parent::__construct($param).
I already spent hours reviewing all the code and can't find what Im doing wrong. Why in the first case the parent constructor is instantiated without being called and on the second case not?
Is this a bug? Any idea what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PHP 中,父构造函数不会被隐式调用。
来源:http://php.net/manual/en/language.oop5.decon .php
In PHP parent constructors aren't called implicitly.
Source: http://php.net/manual/en/language.oop5.decon.php