Home » Php » php – How to tell phpDoc a string is a class name?

php – How to tell phpDoc a string is a class name?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I often give objects static methods and properties that do not require the object to be initialized. For example:

class SomeObject {
    public function __construct($object_id) {
        $this->loadProperties($object_id);
    }

    public static function getSomeStaticString() {
        return "some static string";
    }
}

Now we subclass these objects and have some sort of controller that returns an object class string under certain circumstances where the object should not yet be initialized. For example:

class SomeObjectController {
    public function getSomeObjectWithTheseProperties(array $properties) {
        if($properties[0] === "somevalue") {
            if($properties[1] === "someothervalue") {
                return SomeSubclassObject::class;
            }

            return SomeObject::class;
        }

        return NULL;
    }
}

At times I might want to call the static function SomeObject::getSomeStaticString() without actually initializing the object (because that would involve an unneeded database fetch). For instance:

$controller = new SomeObjectController;
$properties = array("somevalue", "someothervalue");
$object_class = $controller->getSomeObjectWithTheseProperties($properties);

echo $object_class::getSomeStaticString();

Question: can I somehow tell PhpStorm, preferably through phpDoc, that $object_class is a class string of a subclass of SomeObject?

If I tell my IDE it’s a string, it will notify me getSomeStaticString() is an invalid method. On the other hand, if I tell my IDE it’s an instance of SomeObject, it thinks I can access regular non-static methods and properties, which I can’t.

How to&Answers:
/** @var SomeObject $object_class */
$object_class = $controller->getSomeObjectWithTheseProperties($properties);

enter image description here

Sorry, no other way as to tell that it’s an instance of SomeObject.

… if I tell my IDE it’s an instance of SomeObject, it thinks I can access regular non-static methods and properties, which I can’t.

So? Just do not access non-static methods and properties.

Answer:

PHPStan uses the class-string PHPDoc type for this. You can get autocomplete support in PHPStorm for this by using this plugin.