Код: Выделить всё
class MyClass {
private static $instance;
private function __construct() {
}
public static function getInstance() {
if (isset(self::$instance)) {
return self::$instance;
} else {
$c = __CLASS__;
self::$instance = new $c;
return self::$instance;
}
}
}
Код: Выделить всё
class ExtendedClass Extends MyClass {
//cannot touch parent::$instance, since it's private, so must overwrite
private static $instance;
//calling parent::getInstance() would instantiate the parent,
//not the extension, so must overwrite that too
public static function getInstance() {
if (isset(self::$instance)) {
return self::$instance;
} else {
$c = __CLASS__;
self::$instance = new $c;
return self::$instance;
}
}
}
Код: Выделить всё
$myInstance=ExtendedClass::getInstance();
PHP Неустранимая ошибка: вызов частного MyClass::__construct() из контекста
'ExtendedClass'
Но в PHP 5.1.6 все работает как ожидается
Что здесь происходит?
Также: я не писал MyClass, у меня нет возможности сделать конструктор защищенным. Если бы я это сделал, это решило бы проблему, но я не могу.
Подробнее здесь: https://stackoverflow.com/questions/228 ... n-5-1-to-5
Мобильная версия