I would like to do something like this:
abstract class Foo
{
public function __construct()
{
echo 'This is the parent constructor';
}
abstract function __construct();
}
class Bar extends Foo
{
// constructor is required as this class extends Foo
public function __construct()
{
//call parent::__construct() if necessary
echo 'This is the child constructor';
}
}
But I get a fatal error when doing this:
Fatal error: Cannot redeclare Foo::__construct() in Foo.php on line 8
Is there another way to ensure child classes have a constructor?
In short no. Non of the magic methods can be declared via the abstract keyword.
If you want to use the old way of constructors, create a method with the same name as the class, and declare it abstract. This will be called upon instantiation of the class.
Example:
I would suggest the use of interfaces for your functionality though.