Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cannot call constructor

Description

When there is no need to define a constructor, it may be skipped. Then, PHP creates the object without that initial method call.

On the other hand, if a child class makes an explicit call to the parent constructor, then on of the parents MUST have a constructor.

In the example here, the child class explicitly calls the parent constructor, but it doesn’t exists.

Example

<?php

class X {}

class Y extends X {
	function __construct() {
		parent::__construct();
	}
}

?>

Alternatives

  • Create an empty constructure in a parent, to ensure its calling.
  • Skip the call to the parent constructor in the child: it is useless anyway.