Is it possible for an abstract class to force it's children to have a constructor in PHP?
- by Logan Serman
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?