Php 未在子类中继承的静态变量
由于某些原因,我无法在子类中继承静态变量。下面的代码段看起来不错,但不起作用Php 未在子类中继承的静态变量,php,oop,static,isset,defined,Php,Oop,Static,Isset,Defined,由于某些原因,我无法在子类中继承静态变量。下面的代码段看起来不错,但不起作用 abstract class UserAbstract { // Class variables protected $type; protected $opts; protected static $isLoaded = false; protected static $uid = NULL; abstract protected function load();
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");
}
}
}
除了可能的复制粘贴/重新格式化错误外,上面的代码没有父级的静态变量,因此我想我遗漏了某个细节
任何关于正在发生的事情的线索都是值得赞赏的。只适用于常数。您应该使用我看到几个错误。你是说
if (isset(self::$uid))
echo "\$uid: " . self::$uid . " is defined";
else
echo "\$uid is not defined";
更新:
需要明确的是,正如@stefgosselin和@supericy所说,该缺陷是由使用定义的而不是isset
引起的。在php5.3+中添加
因此,在php5.3+中,这将起作用:
if (isset(static::$uid))
echo "\$uid: " . static::$uid . " is defined";
else
echo "\$uid is not defined";
从TrexUserActor
类中,这也将起作用:
if (isset(TrexUserActor::$uid))
echo "\$uid: " . TrexUserActor::$uid . " is defined";
else
echo "\$uid is not defined";
. 你忘了从UserAbstract
派生TrexUserAbstract
?啊,伙计。这很奇怪,因为我和你的小提琴有相同的代码,除了我使用的是Autolode。我有版本5.3.2-1,是的。这是问题所在,谢谢你的朋友。这只是为了有一个快速的调试输出,只是为了在最激烈的时刻指出成功或失败。再次感谢。