cannot define constructor as protected or private! why? cannot define constructor as protected or private! why? php php

cannot define constructor as protected or private! why?


I've just created a simple test-file to confirm whether this happens on my machine too, and I think I've found the answer. Take the following code:

<?phperror_reporting( E_ALL | E_STRICT );class Foo {    public function __construct( ) {    }}class Bar extends Foo {    protected function __construct( ) {    }}

When trying to execute that code, I get a fatal error: "PHP Fatal error: Access level to Bar::__construct() must be public (as in class Foo) in /home/berry/foo.php on line 12." That means you can't change the access level in a child class, if the parent has already defined the access level, which actually makes a lot of sense: PHP wouldn't know which constructor to call, I guess.

As a side note: by looking at your code, B extends A, and A uses B. Why exactly is that so, it seems like a strange construction to me? I'm guessing you actually want is composition, not inheritance.


You can define a constructor as protected or private. This code compiles runs just fine since OOP was rewritten for PHP/5:

<?phpclass A{    public function __construct(){        echo 'Instance of A created' . PHP_EOL;    }}class B{    protected function __construct(){        echo 'Instance of B created' . PHP_EOL;    }}class C{    private function __construct(){        echo 'Instance of C created' . PHP_EOL;    }}

Of course, private constructors prevent you from creating an instance with the new keyword, but PHP will trigger a fatal error (it won't just stop running):

<?phpnew A; // OKnew B; // Fatal error: Call to protected B::__construct() from invalid contextnew C; // Fatal error: Call to private C::__construct() from invalid context


You can create custom static initiators:

<?phpclass FancyClass {    public static function init() {        new self();    }    protected function __construct() {        echo 'Instance created!';    }}# This will not work.new FancyClass();# But this will work.FancyClass::init();