在PHP中具有类型提示的动态返回类型? [英] Dynamic return type with type hint in PHP?

查看:35
本文介绍了在PHP中具有类型提示的动态返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一种标准方法,我知道可以在注释中指定返回类型,例如:

For a standard method, I know one can specify return type in the comments such as:

/**
 * Load this entity from the database with the specified primary key.
 * @param int $Key
 * @return BaseEntity
 */
public static function Load($Key)
{ ... }

我希望根据子类更改返回类型.像这样:

I would like to have the return type change depending on the subclass. Something like:

 * @return __CLASS__

在其他语言中,这可以使用模板等完成.我对PHP有任何选择吗?

In other languages, this could be accomplished using templates etc. Do I have any options for PHP?

推荐答案

否.除非您编写一些代码来生成此代码,否则不会.

No. Not unless you code something to generate this code.

这些是注释,它们不会更改代码的运行方式.您的IDE可以将它们用于代码提示,或者PHPDoc可以将它们用于生成文档.这些应用程序可能具有执行所需功能的方式,请查看其文档.但是,注释不会影响代码的运行方式.

Those are comments, they will not change the way that the code runs. They may be used by your IDE for code hints, or by PHPDoc to generate documentation. Those applications may have ways of doing something like what you want, check their documentation. The comments will not, however, affect the way your code runs.

这篇关于在PHP中具有类型提示的动态返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆