Search code examples
phpoopstaticissetdefined

Static variables not being inherited in child classes


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.


Solution

  • defined only applies to constants. You should be using isset