从 PHP 5 升级到 PHP 7 后,以下代码产生了不同的结果:
abstract class TheParent {
public static $prop;
public function __construct( $val ) {
static::set_prop($val);
}
public static function set_prop( $val ) {
$ref_reset = $val;
static::$prop =& $ref_reset;
}
}
class Child extends TheParent {
// public static $prop; //<-- not declared on purpose
}
class Child2 extends TheParent {
// public static $prop; //<-- not declared on purpose
}
$c = new Child('do');
$c2 = new Child2('re');
echo 'Child: ' . Child::$prop . '<br>';
echo 'Child2: ' . Child2::$prop . '<br>';
echo 'TheParent: ' . TheParent::$prop;
在 PHP5 中:
Child: do
Child2: re
TheParent:
在 PHP7 中:
Child: re
Child2: re
TheParent: re
我想要的输出是 PHP5 输出,因为我希望能够在扩展基(父)类的所有类中引用单个属性名称,但我不想重新声明该属性或在每一个中设置它的方法子类(主要是为了避免为几十个类添加相同的属性/方法的维护开销)。
似乎 PHP5 的魔力在于通过引用分配(在对 SO 进行大量搜索之后,有帮助的 回答者提到通过引用设置“破坏”了“引用集”,它允许每个子类中的属性保存单独的值)。我发现这是 PHP5 中一个非常优雅的解决方案。
有没有办法在 PHP7 中使用相同或相似的代码实现相同的结果?
解决方法:
看起来这是 PHP 7.2 和 7.3 之间发生重大变化的结果,并且可能没有类似优雅的两行替代方案。在重构我的代码之后,我发现这个稍微冗长的解决方法是有效的(并且满足了我不必在子类中重新声明属性的主要目标):
abstract class TheParent {
public static $props = [];
public function __construct( $val ) {
static::set_prop($val);
}
public static function set_prop( $val ) {
self::$props[static::class] = $val;
}
public static function get_prop() {
if( isset(self::$props[static::class]) )
return self::$props[static::class];
}
}
class Child extends TheParent {
// public static $prop; //<-- not declared on purpose
}
class Child2 extends TheParent {
// public static $prop; //<-- not declared on purpose
}
$c = new Child('do');
$c2 = new Child2('re');
echo 'Child: ' . Child::get_prop(). '<br>'; // 'do' in PHP 7.3
echo 'Child2: ' . Child2::get_prop() . '<br>'; // 're' in PHP 7.3
echo 'TheParent: ' . TheParent::get_prop(); // '' in PHP 7.3