Reputation: 9148
For some reason I cannot get static variables to be inherited in child classes. The following snippet seems ok, but does not work.
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");
}
}
}
Aside from the possible copy-paste/reformat errors, the above code does not have the parent`s static variables, so I guess I am missing a detail somewhere.
Any clue on what is happening is appreciated.
Upvotes: 1
Views: 171
Reputation: 10348
I see several errors. You meant?
if (isset(self::$uid))
echo "\$uid: " . self::$uid . " is defined";
else
echo "\$uid is not defined";
UPDATE:
To be clear, as @stefgosselin and @supericy says the bug was caused by use defined
instead isset
. In php5.3+ was added Late Static Bindings.
So in php5.3+ this will work:
if (isset(static::$uid))
echo "\$uid: " . static::$uid . " is defined";
else
echo "\$uid is not defined";
And from out of TrexUserActor
class this will work too:
if (isset(TrexUserActor::$uid))
echo "\$uid: " . TrexUserActor::$uid . " is defined";
else
echo "\$uid is not defined";
Upvotes: 1