Is it possible for an abstract class to force it's children to have a constructor in PHP?

Posted by Logan Serman on Stack Overflow See other posts from Stack Overflow or by Logan Serman
Published on 2012-06-23T03:01:07Z Indexed on 2012/06/23 3:16 UTC
Read the original article Hit count: 117

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about oop