18

大多数 PHP IDE 依赖 phpdoc 来获取有关表达式类型的提示。然而,我经常使用这种模式,这似乎没有被涵盖:

class Control {
    private $label = '';

    /** @return ??? */
    public static function Make(){ return new static(); }

    /** @return ??? */
    public function WithLabel($value){  $this->label = $value;  return $this;  }

    /** @return void */
    public function Render(){ /* ... */ }
}

class Textbox extends Control {
   private $text = '';

    /** @return ??? */
    public function WithText($text){  $this->width = $text;  return $this;  }
}

现在我可以使用这样的类:

Textbox::Make()           // <-- late static binding, returns Textbox
   ->WithLabel('foo')     // <-- late dynamic binding, returns Textbox
   ->WithText('bar')      // <-- normal binding, returns Textbox
   ->Render();

有什么方法可以用某些东西替换'???',以便输入信息正确?

4

3 回答 3

9
/** @return Control */

对于非静态:

/** @return $this */

但它没有记录在 phpdoc 手册中

于 2011-05-02T14:04:36.730 回答
6

更新的答案以最流行的 PHP IDE (PHPStorm 8) 为参考:

因为@return你可以使用:

  • self
  • $this

因为@method你可以使用:

  • $this

例子:

/**
 * Class Model
 * @method $this parentMethodA
 */
class Model
{
    /**
     * @return $this
     */
    public function parentMethodB()
    {
        return $this;
    }

    /**
     * @return self
     */
    public function parentMethodC()
    {
        return $this;
    }
}


/**
 * Class Person
 * @method $this childMethodA
 */
class Person extends Model
{
    /**
     * @return $this
     */
    public function childMethodB()
    {
        return $this;
    }

    /**
     * @return self
     */
    public function childMethodC()
    {
        return $this;
    }
}

$p = new Person();

//In the following lines IDE will recognize those variables as:
$t1 = $p->parentMethodA(); //Instance of Person
$t2 = $p->parentMethodB(); //Instance of Person
$t3 = $p->parentMethodC(); //Instance of Model
$t4 = $p->parentMethodA(); //Instance of Person
$t5 = $p->parentMethodB(); //Instance of Person
$t6 = $p->parentMethodC(); //Instance of Person

PHPStorm 10 (EAP) 更新

似乎 nowstatic也可以使用,但仅限于@return.

于 2014-07-29T13:14:15.380 回答
5

更新了 cvsguimaraes 的答案以包括静态选项:

/**
 * Class Bar
 * @method $this parentMethodA
 */
class Bar
{
    /**
     * @return $this
     */
    public function parentMethodB()
    {
        return $this;
    }

    /**
     * @return self
     */
    public function parentMethodC()
    {
        return $this;
    }

    /**
     * @return static
     */
    public static function staticMethod()
    {
        return new static();
    }

    /**
     * @param $id
     *
     * @return bool
     */
    public function load($id)
    {
        // test
        return $id ? true : false;
    }

    /**
     * @param null $id
     *
     * @return static
     */
    public static function get($id = NULL){
        $obj = static::staticMethod();

        if (is_null($id)) {
            return $obj;
        }

        if ($obj->load($id)) {
            return $obj;
        }

        return false;
    }
}


/**
 * Class Foo
 * @method $this childMethodA
 */
class Foo extends Bar
{
    /**
     * @return $this
     */
    public function childMethodB()
    {
        return $this;
    }

    /**
     * @return self
     */
    public function childMethodC()
    {
        return $this;
    }
}

/**
 * Class Bar
 */
class Baz extends Bar 
{

}

$p = new Foo();

/** @var Foo $Foo */
$Foo = 'Foo';
$Baz = 'Bar';

//  IntelliJ recognizes the following as:
$t1 = $p->parentMethodA(); //Instance of Foo
$t2 = $p->parentMethodB(); //Instance of Foo
$t3 = $p->parentMethodC(); //Instance of Model
$t4 = $p->childMethodA(); //Instance of Foo
$t5 = $p->childMethodB(); //Instance of Foo
$t6 = $p->childMethodC(); //Instance of Foo
$t7 = $Foo::staticMethod(); //Instance of Foo
$t8 = Foo::staticMethod(); //Instance of Foo
$t9 = $p::staticMethod(); //Instance of Foo
$t10 = $Foo::get(); //Instance of Foo
$t12 = Bar::get(); //Instance of Bar
$t11 = $Baz::get(); // Unknown
于 2014-12-29T17:56:36.477 回答