【问题标题】:Set cache handler at object level?在对象级别设置缓存处理程序?
【发布时间】:2013-11-08 04:47:46
【问题描述】:

假设我有一个基础对象,CacheObject:

abstract class CacheObject {
    protected static $handler = null;

    public static function setCacheHandler($handler) {
        static::$handler = $handler;
    }

    public static function getCacheHandler($handler) {
        return static::$handler;
    }
}

class A extends CacheObject {

}

class B extends CacheObject {

}

A::setCacheHandler('test');
var_dump(B::getCacheHandler());

B 会给我“测试”,我认为这是因为 A 类没有自己定义的属性 $handler...所以它使用的是继承的,由 B 类共享。这准确吗?

有什么方法可以让它们分开设置,而不需要在每个对象中声明 $handler?

【问题讨论】:

    标签: php late-binding late-static-binding


    【解决方案1】:

    你的

    public static function getCacheHandler($handler) { // No need to pass arg here
            return static::$handler;
        }
    

    应该是

    public static function getCacheHandler() {
            return static::$handler;
        }
    

    【讨论】:

    • 确实.. 在这个例子中我只是快速复制了它。但它没有回答我原来的问题...... :)
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2011-09-15
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多