【发布时间】:2022-07-26 03:55:35
【问题描述】:
这是一些适用于 php 7.4 但不适用于 php 8.1 的简单代码:
<?php
class A
{
public name = "I'm A";
private function __construct() {}
public static function instance()
{
static $instance;
if (!$instance) {
$instance = new self(); // or new static()
}
return $instance;
}
}
class B extends A
{
public $name = "My name is B";
}
B::instance()->name;
A::instance()->name;
现在,将此代码与 php 7.4 一起使用将给出:
“我叫 B”
“我是A”
使用 php 8.1 运行时会给出:
“我叫 B”
“我叫 B”
我怀疑 php 开发人员有充分的理由进行此更改,我知道单例模式已被弃用,但我需要使用该代码而不返回 php 7.4。
【问题讨论】:
标签: php