cannot define constructor as protected or private! why?
--- A.php ----
require_once 'B.php';
class A
{
public function __constructor($x){...}
public function foo()
{
$b = B::get开发者_运维知识库Instance();
...
}
}
--- B.php ----
require_once 'A.php';
class B extends A
{
protected static $_instance = null;
protected function __construct(){}
public static function getInstance()
{....}
}
PHP just stops interpreting the code as soon as it reaches line
protected function __construct(){}
and outputs everything before and nothing that would have been sent to the browser afterwards.
And as soon as I just take that line out, by changing it to
// protected function __construct(){}
everything works fine!?
I don't get that.
Any ideas?
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:
<?php
error_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:
<?php
class 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):
<?php
new A; // OK
new B; // Fatal error: Call to protected B::__construct() from invalid context
new C; // Fatal error: Call to private C::__construct() from invalid context
You can create custom static initiators:
<?php
class 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();
精彩评论