Phpdoc,如何为代码完成定义一个实际不存在的变量?

Phpdoc, how to define a variable for code completion what doesnt actually exists?

给出这个例子:

class Example
{
    private $var;

    public function method()
    {
        $this->   here the IDE will offer the var
    }
}

但是如果我有这个怎么办:

class Example
{
    //private $var;

    public function method()
    {
        $this->   var will no longer be offered
    }
}

所以换句话说,我希望即使没有实际变量也能完成代码。那是因为我想使用 with __get 方法。不幸的是,我不能使用 unset($this->var).

由于这一点,如果我们将使用受保护的变量,然后将此变量用于相同的调用以及子 class。

这是您使用 @property 标签的一个很好的例子。它甚至提到了魔术方法 __get__set.

的例子

在您的情况下,它可能类似于以下内容:

<?php

class Example
{
    /**
     * @property string $var A variable that can be set and gotten with the magic methods
     */

    public function method()
    {
        $this->var; //here the IDE will offer the var
    }
    
    public function __get($name)
    {
        return $this->$name;
    }
}

?>

另外,请注意以下几点:

The magic methods are not substitutes for getters and setters. They just allow you to handle method calls or property access that would otherwise result in an error. As such, there are much more related to error handling. Also note that they are considerably slower than using proper getter and setter or direct method calls.
Gordon on PHP __get and __set magic methods