gpt4 book ai didi

php - 如何记录 IDE 的魔法(_call 和 _callStatic)方法

转载 作者:IT老高 更新时间:2023-10-28 11:55:20 27 4
gpt4 key购买 nike

在使用 notepad++ 和 sublime 编码多年后,有人建议我试一试 PHP IDE。我正在尝试 phpStorm,它看起来不错。代码完成和文档是一个很棒的功能,但在使用魔术方法时对我来说不起作用。 有没有办法让 phpStorm 了解魔术方法中发生了什么?

我们的情况是这样的:

abstract class a {
public static function __callStatic($method,$args)
{
if(strpos($method,"get_by_") === 0)
{
//do stuff
} elseif(strpos($method,"get_first_by_") === 0) {
//do stuff
} elseif($method == "get_all") {
//do stuff
}
}
}

class b extends a {
// some more stuff
}

b::get_by_user_id(27);
b::get_first_by_id(156);
b::get_all();

神奇的 callStatic 方法允许我们通过构成函数调用的 1 个或多个参数来获取对象集合。

我看到在这些情况下有一个@method 语句可以使用,但是 phpStorm 只选择了这些语句中的第一个。此外,我只能将返回类型设置为混合,因为我希望能够将其设置为调用它的任何类(在我的示例中为 b)。

任何想法或建议都将非常感激,谢谢。

最佳答案

使用类级别的 PHPDoc 注释——特别是 @method 标签——在 PhpStorm 中可以正常工作:

/**
* @method static someClass get_by_user_id(int $id) Bla-bla
* @method static someClass get_first_by_id(int $id)
*/
abstract class a {
...

在上面:

  • @method -- PHPDoc 标签
  • static -- 告诉这是静态方法
  • someClass$this -- 返回类型
  • get_by_user_id -- 方法名
  • (int $id) -- 方法签名:([[type] [parameter]<, ...>])
  • Bla-bla -- 一些可选的描述

更多关于@method :

附言@method static在 PhpStorm 中工作正常(告诉 IDE 该方法是静态的)它可能(还没有?)实际的 phpDocumentor 工具不支持(抱歉,暂时没有使用它)。


或者:(当然是在 PhpStorm 中)Settings | Inspections | PHP | Undefined | Undefined method --> Downgrade severity if __magic methods are present in class -- 它不会以任何方式帮助此类方法的代码完成,但不会将这些魔术方法标记为“未定义方法”错误。


phpDocumentor 的工单 关于使用正则表达式/部分名称用于 @property/@method标签(它对文档有什么用,以及在处理代码完成时它对实际 IDE 的帮助有多大):

关于php - 如何记录 IDE 的魔法(_call 和 _callStatic)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com