如何告诉phpDoc一个字符串是一个类名?
我经常给对象静态方法和属性,不需要初始化对象.例如:
class SomeObject { public function __construct($object_id) { $this->loadProperties($object_id); } public static function getSomeStaticString() { return "some static string"; } } 现在我们对这些对象进行子类化,并使用某种控制器在某些情况下返回对象类字符串,在这种情况下,对象尚未初始化.例如: class SomeObjectController { public function getSomeObjectWithTheseProperties(array $properties) { if($properties[0] === "somevalue") { if($properties[1] === "someothervalue") { return SomeSubclassObject::class; } return SomeObject::class; } return NULL; } } 有时我可能想要调用静态函数SomeObject :: getSomeStaticString()而不实际初始化对象(因为这将涉及不需要的数据库提取).例如: $controller = new SomeObjectController; $properties = array("somevalue","someothervalue"); $object_class = $controller->getSomeObjectWithTheseProperties($properties); echo $object_class::getSomeStaticString(); 问题:我能以某种方式告诉PhpStorm,最好通过phpDoc,$object_class是SomeObject的子类的类字符串吗? 如果我告诉我的IDE它是一个字符串,它会通知我getSomeStaticString()是一个无效的方法.另一方面,如果我告诉我的IDE它是SomeObject的一个实例,它认为我可以访问常规的非静态方法和属性,我不能. 解决方法/** @var SomeObject $object_class */ $object_class = $controller->getSomeObjectWithTheseProperties($properties); 对不起,没有其他方法可以告诉它是SomeObject的一个实例.
所以?只是不要访问非静态方法和属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |