2
比方说,我已经PHPDoc的,工厂返回:键入
- 一个抽象类,用一个抽象方法。
- 2个以自己的方式定义抽象方法的子类。
- 一个基于参数返回2个子类之一的实例的工厂。
下面是示例代码
abstract class Datalist{
abstract public function render($arg1, $arg2);
}
class Datalist_Table{
public function render($arg1, $arg2){
/* do something here */
}
}
class Datalist_List{
public function render($arg1, $arg2){
/* do something here */
}
}
class DatalistFactory{
/**
* usual stuff
*
* @return Datalist
*/
public static function build($args){
$class_name = 'Datalist_' . $args['type'];
return new $class_name($args['m'][0], $args['m'][1]);
}
}
//in some other file
$list = DatalistFactory::build($args);
$list-> ....
我的问题
我的IDE(PHPStorm)不会对$list->render()
暗示。我的猜测是,因为它已被宣布为abstract
。
问题
我应该摆在前面@return
在PHPDoc的为DatalistFactory::build()
什么,这样在子类中定义以及功能的IDE提示。
P.S.
- 我试过在说明以下几个问题,但没有成功
phpDoc notation to specify return type identical to parameter type - PHPDoc: Is is possible to reference the object property descriptions from factory method docblock?
- PHPDoc preconditions
- PHPDoc for fluent interface in subclass?
我不正常使用IDE,它使用doc块提出这样的方法和东西? –
您可以使用[元数据方法](https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata)或使用“DynamicReturnType插件”..但它不适用于您的工厂 - 您将所有参数作为数组传递而不是单独的参数。 – LazyOne
因此 - 或者使用已经建议的'@return Datalist'(将不会提示子特定的方法..但会为'Datalist'类方法做)..或者在创建'$ list'时提供类型提示,例如'/ ** @var Datalist_List $ list * /''$ list = DatalistFactory :: build($ args);' – LazyOne