出於某種原因,我無法獲得要在子類中繼承的靜態變量。以下片段似乎沒問題,但不起作用。未在子類中繼承的靜態變量
abstract class UserAbstract {
// Class variables
protected $type;
protected $opts;
protected static $isLoaded = false;
protected static $uid = NULL;
abstract protected function load();
abstract protected function isLoaded();
}
class TrexUserActor extends TrexUserAbstract {
// protected static $uid; // All is well if I redefine here, but I want inheritance
/**
* Constructor
*/
public function __construct() {
$this->load();
}
protected function load() {
if (!$this->isLoaded()) {
// The following does NOT work. I expected self::$uid to be available...
if (defined(static::$uid)) echo "$uid is defined";
else echo self::$uid . " is not defined";
echo self::$uid;
exit;
// Get uid of newly created user
self::$uid = get_last_inserted_uid();
drupal_set_message("Created new actor", "notice");
// Flag actor as loaded
self::$isLoaded = true;
variable_set("trex_actor_loaded", self::$uid);
} else {
$actor_uid = variable_set("trex_actor_uid", self::$uid);
kpr($actor_uid);
exit;
$actor = user_load($actor_uid);
drupal_set_message("Using configured trex actor ($actor->name)", "notice");
}
}
}
從可能的複製粘貼/格式化錯誤
除此之外,上面的代碼不具備parent`s靜態變量,所以我想我的地方缺少一個細節。
任何有關正在發生的事情的線索都是值得讚賞的。
哪個版本的PHP? – Eric
[Works for me](http://sandbox.onlinephpfunctions.com/code/27c3e6f4075ac9e9c306bdfe9203df4d7b327884)。你忘了從'UserAbstract'派生'TrexUserAbstract'嗎? – Eric
啊人。這很奇怪,因爲除了我使用autoloade之外,我的代碼與您的小提琴完全相同。我有版本5.3.2-1, – stefgosselin