Why am I getting Fatal error when calling a parent's constructor?

后端 未结 5 2165
执念已碎
执念已碎 2021-02-11 17:07

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 erro

5条回答
  •  有刺的猬
    2021-02-11 18:01

    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 () 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).

提交回复
热议问题