让 PhpStorm 意识到继承的单例 类
Make PhpStorm Aware of inherited Singleton Classes
我在 Parent Class 中有以下代码:
class parent {
/**
* Memory of the instances of the classes.
* @since 1.0.0
* @access protected
* @static
* @var array
*/
protected static $instances = [];
/**
* Singleton
* Thanks to:
* @since 1.0.0
* @access public
* @static
* @return object
*/
public static function instance() {
if ( empty( self::$instances[static::class] ) ) {
$instance = new static();
self::$instances[static::class] = $instance;
} else {
$instance = self::$instances[static::class];
}
return $instance;
}
}
ChildClass:
class child extends parent {
public function test() {
}
}
我可以使用此代码执行以下操作:
$class_child = child::instance();
但是 PhpStorm 不知道 test()
方法。
如果我写 $class_child->
,则不会列出任何建议。我能做什么?
此处提到的解决方案不适用于我的情况。
在 PHPDoc 中为 instance()
方法使用 @return static
而不是 @return object
。
现在 PHPDoc 告诉这个方法return一些对象(可以是任何对象)。
使用 @return static
它将 return class 的实例,其中使用了 instance()
方法。因此对于 ChildClass
它将被解释为 @return ChildClass
,对于 GrandChildClass
它将被解释为 @return GrandChildClass
.
我在 Parent Class 中有以下代码:
class parent {
/**
* Memory of the instances of the classes.
* @since 1.0.0
* @access protected
* @static
* @var array
*/
protected static $instances = [];
/**
* Singleton
* Thanks to:
* @since 1.0.0
* @access public
* @static
* @return object
*/
public static function instance() {
if ( empty( self::$instances[static::class] ) ) {
$instance = new static();
self::$instances[static::class] = $instance;
} else {
$instance = self::$instances[static::class];
}
return $instance;
}
}
ChildClass:
class child extends parent {
public function test() {
}
}
我可以使用此代码执行以下操作:
$class_child = child::instance();
但是 PhpStorm 不知道 test()
方法。
如果我写 $class_child->
,则不会列出任何建议。我能做什么?
此处提到的解决方案不适用于我的情况。
在 PHPDoc 中为 instance()
方法使用 @return static
而不是 @return object
。
现在 PHPDoc 告诉这个方法return一些对象(可以是任何对象)。
使用 @return static
它将 return class 的实例,其中使用了 instance()
方法。因此对于 ChildClass
它将被解释为 @return ChildClass
,对于 GrandChildClass
它将被解释为 @return GrandChildClass
.