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