为什么调用父构造函数时会出现致命错误? [英] Why am I getting Fatal error when calling a parent's constructor?

查看:421
本文介绍了为什么调用父构造函数时会出现致命错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了一个SPL(标准PHP库)类,我无法调用父的构造函数。这是我得到的错误:


致命错误:无法调用构造函数


这是指向 SplQueue 的文档的链接: http://www.php.net/manual/en/class.splqueue.php



这里是我的代码:

  $ queue = new Queue(); 

class Queue extends SplQueue {

public function __construct(){
echo'before';
parent :: __ construct();
echo'我已经在父构造函数调用';
}

}

退出;什么可能阻止我调用父的构造函数?

=h2_lin>解决方案

SplQueue 继承自 SplDoublyLinkedList 。这些类都不定义自己的构造函数。因此,没有显式的父构造函数来调用,你会得到这样的错误。文档在这一点上有点误导(因为它是许多SPL类)。



要解决错误,不要调用父构造函数。






现在,在大多数面向对象语言中,你会期望默认构造函数没有在类中声明的显式构造函数。但是这里的catch: PHP类没有默认构造函数。一个类有一个构造函数 if和只有一个被定义



事实上,使用反射来分析 stdClass 类,我们看到即使没有构造函数:

  $ c = new ReflectionClass('stdClass'); 
var_dump($ c-> getConstructor()); // NULL

尝试反映 SplQueue的构造函数 SplDoublyLinkedList 都会产生 NULL



我的猜测是,当你告诉PHP实例化一个类,它执行所有的内部存储器分配,它需要为新的对象,然后寻找一个构造函数定义,并调用它只有定义 __ construct()< class name>()。我去看看源代码,看起来PHP只是出了问题,当它不能找到一个构造函数调用,因为你在一个子类中明确地告诉它(见 zend_vm_def。 h )。


I am extending one of the SPL (Standard PHP Library) classes and I am unable to call the parent's constructor. Here is the error I am getting:

Fatal error: Cannot call constructor

Here is a link to the SplQueue's documentation: http://www.php.net/manual/en/class.splqueue.php

Here is my code:

$queue = new Queue();

class Queue extends SplQueue {

    public function __construct() {
        echo 'before';
        parent::__construct();
        echo 'I have made it after the parent constructor call';
    }

}

exit;

What could prevent me from calling the parent's constructor?

解决方案

SplQueue inherits from SplDoublyLinkedList. Neither of these classes defines a constructor of its own. Therefore there's no explicit parent constructor to call, and you get such an error. The documentation is a little misleading on this one (as it is for many SPL classes).

To solve the error, don't call the parent constructor.


Now, in most object-oriented languages, you'll expect the default constructor to be called if there isn't an explicit constructor declared in a class. But here's the catch: PHP classes don't have default constructors! A class has a constructor if and only if one is defined.

In fact, using reflection to analyze the stdClass class, we see even that lacks a constructor:

$c = new ReflectionClass('stdClass');
var_dump($c->getConstructor()); // NULL

Attempting to reflect the constructors of SplQueue and SplDoublyLinkedList both yield NULL as well.

My guess is that when you tell PHP to instantiate a class, it performs all the internal memory allocation it needs for the new object, then looks for a constructor definition and calls it only if a definition of __construct() or <class name>() is found. I went to take a look at the source code, and it seems that PHP just freaks out and dies when it can't find a constructor to call because you told it explicitly to in a subclass (see zend_vm_def.h).

这篇关于为什么调用父构造函数时会出现致命错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆