首页 > 解决方案 > 有没有办法在@property-read 中使用对象类型并“声明”它的属性?

问题描述

我想在其中添加一种对象,@property-read并且还能够通过 PhpStorm 建议访问其属性。

这是我想做的事情:

/**
* @property-read object{type: string, schema: string} $request_schema
* @property-read object{type: string, schema: string} $response_schema
*/
class ConfigApiRoutesSchema extends BaseSchema
{
...
}

我这样做的原因是因为我想访问属性类型和架构,如下所示:

$configApiRoutesSchema = new ConfigApiRoutesSchema();
$configApiRoutesSchema->request_schema->type // Here's the problem, type is not suggested
$configApiRoutesSchema->request_schema->schema // schema is also not suggested

这正是我所需要的,但不幸的是,由于某种原因这不起作用。一种解决方案是创建两个类并将它们指定为类型:

/**
* @property-read string $type
* @property-read string $schema
*/
class RequestSchema {}
    
/**
* @property-read string $type
* @property-read string $schema
*/
class ResponseSchema {}
    

    
/**
* @property-read RequestSchema $request_schema
* @property-read ResponseSchema $response_schema
* @
*/
class ConfigApiRoutesSchema extends BaseSchema {}

现在 PhpStorm 建议模式和类型。

但这似乎不正确,因为RequestSchema并且ResponseSchema实际上不会被使用。所以我想知道是否有任何方法可以对对象使用注释,而不必创建一个类来建议它的属性。

谢谢你。

标签: phpannotationsphpstormphpdoc

解决方案


推荐阅读