PHP 5.3:当在父类中定义而在子类中丢失时,后期静态绑定不适用于属性 [英] PHP 5.3: Late static binding doesn't work for properties when defined in parent class while missing in child class

查看:56
本文介绍了PHP 5.3:当在父类中定义而在子类中丢失时,后期静态绑定不适用于属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个例子,并注意指示的输出.

Take a look at this example, and notice the outputs indicated.

<?php

class Mommy
{
    protected static $_data = "Mommy Data";

    public static function init( $data )
    {
        static::$_data = $data;
    }

    public static function showData()
    {
        echo static::$_data . "<br>";
    }
}

class Brother extends Mommy
{
}

class Sister extends Mommy
{
}

Brother::init( "Brother Data" );
Sister::init( "Sister Data" );

Brother::showData(); // Outputs: Sister Data
Sister::showData(); // Outputs: Sister Data

?>

我的理解是,使用 static 关键字将引用子类,但是显然,只要子类中缺少子类,它就神奇地适用于父类. (这对PHP来说是一种危险的行为,更多内容将在下面说明.)

My understanding was that using the static keyword would refer to the child class, but apparently it magically applies to the parent class whenever it is missing from the child class. (This is kind of a dangerous behavior for PHP, more on that explained below.)

关于要执行此操作的原因,我谨记以下两点:

I have the following two things in mind for why I want to do this:

  1. 我不希望在所有子类中都定义所有属性.
  2. 我希望在父类中将属性定义为默认值,并且希望子类定义能够在需要时覆盖这些属性.每当需要使用默认值时,子类都需要排除属性,这就是为什么我在上面的示例中未在子类中定义属性.

但是,如果我们想在运行时重写属性(通过init方法),它将为父类重写它!从那时起,较早初始化的子类(例如Brother)在您身上发生了意外的变化.

However, if we are wanting to override a property at runtime (via the init method), it will override it for the parent class! From that point forward, child classes initialized earlier (as in the case of Brother) unexpectedly change on you.

显然,这是由于子类没有在子类内部未明确定义时就没有自己的static属性副本的结果-而是引发了错误,而是切换了 static行为,而不是抛出错误访问父级.因此,是否有某种方法可以使父类动态创建一个属于子类的属性,而不会出现在子类定义中?这样子类可以拥有自己的副本.可以使用static属性和static关键字正确引用它,并且可以在编写时考虑到父属性的默认值.

Apparently this is a result of child classes not having their own copy of the static property whenever it isn't explicitly defined inside of the child class--but instead of throwing an error it switches behavior of static to access the parent. Therefore, is there some way that the parent class could dynamically create a property that belongs to the child class without it appearing inside of the child class definition? That way the child class could have its own copy of the static property and the static keyword can refer to it properly, and it can be written to take into account parent property defaults.

还是有其他解决方案,好,坏或丑陋?

Or is there some other solution, good, bad, or ugly?

推荐答案

它确实引用了正确的类,只是除非重新声明或以其他方式破坏了引用集,否则子类中的静态属性与在超类中.

It does refer to the correct class, it's just that, unless redeclared or otherwise the reference set is broken, static properties in subclasses are in the same reference set as in the superclass.

所以您必须这样做:

class Brother extends Mommy
{
    protected static $_data;
}

或:

class Brother extends Mommy
{
}

$tmp = null;
Brother::$_data =& $tmp;
unset($tmp);

这篇关于PHP 5.3:当在父类中定义而在子类中丢失时,后期静态绑定不适用于属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆