在没有重新声明的情况下继承子类中的静态属性? [英] Inherit static properties in subclass without redeclaration?

查看:117
本文介绍了在没有重新声明的情况下继承子类中的静态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到与这个人和我正在写的应用程序。问题是静态属性不是在子类中继承的,所以如果我在我的主类中使用static ::关键字,它也会在我的主类中设置变量。

I'm having the same problem as this guy with the application I'm writing right now. The problem is that static properties are not being inherited in subclasses, and so if I use the static:: keyword in my main class, it sets the variable in my main class as well.

如果我在子类中重新声明静态变量,它可以工作,但我希望有大量的静态属性和子类,并希望避免代码重复。我链接的页面上排名最高的响应有一些指向解决方法的链接,但似乎有404个。任何人都可以借给我一些帮助,或者可能指出我所说的解决方法吗?

It works if I redeclare the static variables in my subclass, but I expect to have a large number of static properties and subclasses and wish to avoid code duplication. The top-rated response on the page I linked has a link to a few "workarounds", but it seems to have 404'd. Can anyone lend me some help or perhaps point me in the direction of said workarounds?

推荐答案

我不确定具体的解决方法是什么它在谈论,我可以想到很多可行的。我个人不会在任何代码中使用这些。我建议你看一看是否有可能过度使用PHP中的后期静态绑定?并重新考虑是否有更好的方法来做任何你希望完成的事情。

I'm not sure what specific workaround it was talking about, I can think of quite a few that can work. I personally wouldn't use these in any code. I'd recommend you take a look Is it possible to overuse late static binding in PHP? and rethink if there's a better way to do whatever you hope accomplish.

还要记住我的代码完全未经测试我在这里写了它。

Also keep in mind my code is completely untested as I wrote it just here.

所有其他方法都是基于这个。无论您使用静态属性,都可以插入代码来检测类并获取它。如果您从未计划在其他地方使用该物业,我只会考虑这一点。

All the other methods are based on this one. Whereever you use the static property, you insert the code to detect the class and get it. I would only consider this if you never plan on using the property anywhere else.

$class = get_called_class();
if(isset(self::$_names[$class])) {
    return self::$_names[$class];
}
else {
    return static::NAME_DEFAULT;
}



方法2:使用getter / setting方法



如果你计划在多个地方使用它,这种方法会更好。一些单例模式使用类似的方法。

Method 2: Use a getter/setting methods

If you plan on having it used in more than one spot, this method would be better. Some singleton patterns use a similar method.

<?php
class SomeParent {
    const NAME_DEFAULT = 'Whatever defaults here';

    private static $_names = array();

    static function getName($property) {
        $class = get_called_class();
        if(isset(self::$_names[$class])) {
            $name self::$_names[$class];
        }
        else {
            $name = "Kandy"; // use some sort of default value
        }
    }

    static function setName($value) {
        $class = get_called_class();
        self::$_names[$class] = $value;
    }
}



方法3:__ callStatic



这是迄今为止最方便的方法。但是,您需要有一个对象实例才能使用它(__get和__set不能静态使用)。它也是最慢的方法(比其他两个慢得多)。我猜测,因为你已经在使用静态属性,所以这已经不是一个选择了。 (如果这种方法适合你,如果你不使用静态属性可能会更好)

Method 3: __callStatic

This is by far the most convenient method. However you need to have an object instance to use it (__get and __set can't be used statically). It is also slowest the method (much slower than the other two). I'm guessing that since you're already using static properties this is already a non-option. (If this method works for you, it'd be probably be better if you didn't use static properties)

<?php
class SomeParent {

    const NAME_DEFAULT = 'Whatever defaults here';

    private static $_names = array();

    function __get($property) {
        if($property == 'name') {
            $class = get_called_class();
            if(isset(self::$_names[$class])) {
                return self::$_names[$class];
            }
            else {
                return static::NAME_DEFAULT;
            }
        }
        // should probably trigger some sort of error here
    }

    function __set($property, $value) {
        if($property == 'name') {
            $class = get_called_class();
            self::$_names[$class] = $value;
        }
        else {
            static::$property = $value;
        }
    }
}

这篇关于在没有重新声明的情况下继承子类中的静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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