Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 947
Tonto McGee Avatar asked Jan 10 '11 19:01

Tonto McGee


People also ask

How do you call a parent constructor?

In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private). $obj = new OtherSubClass();

How can you call a constructor for a parent class in Wordpress?

Define a constructor in the child class To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments) syntax. The syntax for calling the parent constructor is the same as a regular method.

How can I call the constructor of a parent class from a child class?

In Inheritance, the child class acquires the properties of the base class or parent class. You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class. Example: Javascript.

Does child class always call parent constructor?

That depends on what you mean by "use." If you mean, does the default constructor for a child class call the parent constructor, then yes, it does (more below). If you mean, is a default constructor matching whatever parameters the parent constructor has created automatically, then no, not in the general case.


2 Answers

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

like image 196
BoltClock Avatar answered Oct 06 '22 00:10

BoltClock


This error gets thrown, usually, when the parent class being referenced in parent::__construct() actually has no __construct() function.

like image 43
Kristian Avatar answered Oct 06 '22 00:10

Kristian