0

I'm trying to write some abstract MVC classes in PHP using PHPStorm. Some of the overriding class properties I am using for generic class wrappers tend to be better suited to using static methods and properties. The code works fine, but whenever I use a variable to represent a class name when pointing it at a static variable (one that exists in inherited methods) PHPStorm doesn't know how to handle it and shows it as an error in the IDE. I'm just wondering if there's a better way to do this sort of thing or if it's simply going to be something I have to learn to ignore in the IDE.

Example:

class AbstractModel {
    protected static $_dataMapper     = null;
    protected static $_subs    = null;

    public function __construct($data=null) {
        // constructor omitted with things like a generic class init, etc.
    }

    // $_subs is a static array who's value is a class name use for
    // populating sub-objects as part of a class instance
    public function setSubProperty($subKeyName,$value) {
        $dataType = get_called_class();

        /*************************************
         * PHPStorm complains here on $_subs *
         *************************************/
        if(is_array($dataType::$_subs)
            && array_key_exists($subKeyName,$dataType::$_subs)) {

            $setMethod = 'set' . ucfirst($subKeyName);
            return $dataType->$setMethod($value);
        } else {
            return false; // or throw an exception
        }
    }
}

class SomedataModel extends AbstractModel {
    public $other = null;
    protected static $_subs = array(
        'other'   => "OtherModel"
    );

    public function __construct($data=null) {
        parent::__construct($data=null);
        foreach(array_keys(self::$_subs) as $_k) {
            $setMethod = 'set'.ucfirst($_k);
            $this->$setMethod();
        }
    }

    public function setOther($data=null) {
        // sanitize and set instance of 'other' as $this->other
    }
}
Scott
  • 7,983
  • 2
  • 26
  • 41

1 Answers1

1

You can easily work around that by using the static keyword instead:

...

public function setSubProperty($subKeyName,$value) {

    if (is_array(static::$_subs)
        && array_key_exists($subKeyName, static::$_subs)) {

        $setMethod = 'set' . ucfirst($subKeyName);

        ...

PHPStorm supports it very well. Support of variable string values as classnames resolution of static members and properties is not supported. You might want to open a feature request (if it does not yet exists), however I doubt it's technically feasible because it wouldn't be type-hinting but value-hinting which I think is not supported in Phpstorm.

hakre
  • 193,403
  • 52
  • 435
  • 836