【发布时间】:2021-06-11 13:40:51
【问题描述】:
使用单例子类化,我得到了一个
致命错误:声明 TheChild::getInstance(): TheChild must be compatible with TheParent::getInstance(): TheParent in ..\test.php on line 36
使用 PHP 7.4.x NTS x64 一切正常。
使用 PHP 8.0.x NTS x64 也很好。
PHP 7.3.x NTS x64 是坏孩子。
这是我的代码:
error_reporting(E_ALL);
class TheParent {
private static $instance;
private function __construct() {
// ...
}
public static function getInstance(): self {
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
}
class TheChild extends TheParent {
private static $instance;
private function __construct() {
parent::__construct();
// ...
}
public static function getInstance(): self {
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
}
我只是不确定,我在这里遗漏了一些东西......有人有什么见解吗?客户使用的是 7.3.x,所以我坚持。
【问题讨论】:
标签: php singleton subclassing