加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP特征 – 更改继承类中静态属性的值

发布时间:2020-12-13 13:10:41 所属栏目:PHP教程 来源:网络整理
导读:所以,这是我的特点: trait Cacheable{ protected static $isCacheEnabled = false; protected static $cacheExpirationTime = null; public static function isCacheEnabled() { return static::$isCacheEnabled Cache::isEnabled(); } public static funct
所以,这是我的特点:
trait Cacheable
{
    protected static $isCacheEnabled = false;
    protected static $cacheExpirationTime = null;

    public static function isCacheEnabled()
    {
        return static::$isCacheEnabled && Cache::isEnabled();
    }

    public static function getCacheExpirationTime()
    {
        return static::$cacheExpirationTime;
    }
}

这是基类:

abstract class BaseClass extends SomeOtherBaseClass
{
    use Cacheable;
    ...
}

这是我的2个最后课程:

class Class1 extends BaseClass
{
    ...
}

class Class2 extends BaseClass
{
    protected static $isCacheEnabled = true;
    protected static $cacheExpirationTime = 3600;
    ...
}

以下是执行这些类的代码部分:

function baseClassRunner($baseClassName)
{
    ...
    $output = null;
    if ($baseClassName::isCacheEnabled()) {
        $output = Cache::getInstance()->get('the_key');
    }
    if ($output === null) {
        $baseClass = new $baseClassName();
        $output = $baseClass->getOutput();
        if ($baseClassName::isCacheEnabled()) {
            Cache::getInstance()->set('the_key',$output);
        }
    }
    ...
}

此代码不起作用,因为PHP抱怨在Class2中定义与Cacheable中相同的属性.我无法在它们的构造函数中设置它们,因为我想在运行构造函数之前读取它们.我是开放的想法,任何帮助将不胜感激.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读