PHP:变量函数名(函数指针)调用;如何告诉 IDE 我的函数被调用了?

标签 php phpstorm phpdoc variable-names

我目前正在尝试删除我项目中的所有错误和警告,这是我的 PHPStorm 中的检查工具提供给我的。

我遇到了一段 PHPStorm 说“Unused private method _xxx”,而它实际上是在动态使用的。这是一个简化的片段:

<?php
class A
{
    private function _iAmUsed()
    {
        //Do Stuff...
    }

    public function run($whoAreYou)
    {
        $methodName = '_iAm' . $whoAreYou;
        if (method_exists($this, $methodName)) {
            $this->$methodName();
        }
    }
}

$a = new A();
$a->run('Used');
?>

在这段代码中,PHPStorm 将告诉我“未使用的私有(private)方法 _iAmUsed”,而实际上,它已被使用... 我怎样才能通过添加 PHPDocs 或其他任何东西让我的 IDE 理解我的方法实际被使用了?

请注意,我为“运行”调用提供了一个静态字符串,但我们也可以这样想象:

<?php
$a->run($_POST['whoYouAre']); //$_POST['whoYouAre'] == 'Used'
?>

非常感谢!

最佳答案

在phpdoc中将使用过的方法标记为@used 例子

/**
* @uses  _iAmUsed()
* @param string $whoAreYou
*/ 
public function run($whoAreYou)
{
    $methodName = '_iAm' . $whoAreYou;
    if (method_exists($this, $methodName)) {
        $this->$methodName();
    }
}

关于PHP:变量函数名(函数指针)调用;如何告诉 IDE 我的函数被调用了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57380374/

相关文章:

PHPDoc 和 PhpStorm : indicate the return type of a magic method

php - codeigniter 事件记录,生成,但不执行查询

intellij-idea - 如何防止 IntelliJ IDEA/PhpStorm/Pycharm/Rubymine 中的转义键关闭弹出窗口?

php - 配置PhpStorm连接mysql

phpstorm - 如何在phpstorm中格式化完整的项目源代码?

xslt - 防止将 xmlns =""属性添加到 XSLT 1.0 生成的 HTML 元素中

PHPDocumentor 2 和 PHP 7 在 Doctrine 中有 opcache 问题

php - 我可以使用 &lt;input type ="button"/> 提交表单值吗?

php - Laravel Carbon 本地化不起作用(从数字中获取本地化的月份名称)

php - 我应该为 PDO 使用哪种类型的占位符以获得更快的性能?