简体   繁体   English

"PHP 动态返回类型提示"

[英]PHP dynamic return type hinting

Suppose I have the following PHP function:假设我有以下 PHP 函数:

/**
 * @param string $className
 * @param array $parameters
 * @return mixed
 */
function getFirstObject($className, $parameters) {
    // This uses a Doctrine DQl builder, but it could easily replaced
    // by something else. The point is, that this function can return
    // instances of many different classes, that do not necessarily
    // have common signatures.
    $builder = createQueryBuilder()
        ->select('obj')
        ->from($className, 'obj');
    addParamClausesToBuilder($builder, $parameters, 'obj');
    $objects = $builder
        ->getQuery()
        ->getResult();
    return empty($objects) ? null : array_pop($objects);
}

Basically, the function always returns either an instance of the class specified with the $className parameter or null , if something went wrong.基本上,如果出现问题,该函数总是返回使用$className参数或null指定的类的实例。 The only catch is, that I do not know the full list of classes this function can return.唯一的问题是,我不知道这个函数可以返回的类的完整列表。 (at compile time) (在编译时)

Is it possible to get type hinting for the return type of this kind of function?是否可以获得此类函数的返回类型的类型提示?

In Java, I would simply use generics to imply the return type:在 Java 中,我会简单地使用泛型来暗示返回类型:

static <T> T getOneObject(Class<? extends T> clazz, ParameterStorage parameters) {
    ...
}

I am aware of the manual type hinting, like我知道手动类型提示,例如

/** @var Foo $foo */
$foo = getOneObject('Foo', $params);

but I would like to have a solution that does not require this boilerplate line.但我想有一个不需要这个样板行的解决方案。

To elaborate: I am trying to write a wrapper around Doctrine, so that I can easily get the model entities that I want, while encapsulating all the specific usage of the ORM system.详细说明:我正在尝试围绕 Doctrine 编写一个包装器,以便我可以轻松获得我想要的模型实体,同时封装 ORM 系统的所有特定用法。 I am using PhpStorm.我正在使用 PhpStorm。

** edited function to reflect my intended usage. ** 编辑功能以反映我的预期用途。 I originally wanted to keep it clean of any specific use case to not bloat the question.我最初想保持它没有任何特定的用例,以免问题膨胀。 Also note, that the actual wrapper is more complex, since I also incorporate model-specific implicit object relations and joins ect.另请注意,实际的包装器更复杂,因为我还合并了特定于模型的隐式对象关系和连接等。

这现在可以通过 IntellJ (IDEA/phpStorm/webStorm) 插件 DynamicReturnTypePlugin 实现: https : //github.com/pbyrne84/DynamicReturnTypePlugin

I use phpdoc @mehtod for this purpose.为此,我使用 phpdoc @mehtod For example, I create AbstractRepository class which is extend by other Repository classes.例如,我创建了由其他 Repository 类扩展的AbstractRepository类。 Suppose we have AbstractRepository::process(array $results) method whose return type changes according to the class that extends it.假设我们有AbstractRepository::process(array $results)方法,它的返回类型根据扩展它的类而改变。 So in sub class:所以在子类中:

/**
* @method Car[] process(array $results)
*/
class CarRepo extends AbstractRepository {
    //implementation of process() is in the parent class
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM