所以,这是我的特点:
- 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中相同的属性.我无法在它们的构造函数中设置它们,因为我想在运行构造函数之前读取它们.我是开放的想法,任何帮助将不胜感激.