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.
Advertisement
Answer
/** @var SomeObject $object_class */ $object_class = $controller->getSomeObjectWithTheseProperties($properties);

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.
UPDATE 2021-10-26: Since v2020.3 PhpStorm has Psalm Support and PHPStan Support plugins bundled. They support most of the syntax/types supported by those tools (you can check PhpStorm Issue Tracker here for all the tickets for those plugins (resolved and still pending)).
With those tools you can use class-string pseudo type: