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
    }
}
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.