PHPDoc and late (static or dynamic) binding
Asked Answered
T

3

20

Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered:

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;  }
}

Now I can use the classes like this:

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

Is there any way to replace the '???'s with something so that the typing information is correct?

Tetrad answered 2/5, 2011 at 13:56 Comment(0)
I
12

For static methods in classes that may be extended:

/** @return static */

For final-static methods:

/** @return Control */

For non-static methods:

/** @return $this */

but it's not documented in phpdoc manual

Note that nowadays any Intelij IED (like PhpStorm 2019), does support all three static, $this, and self (as return type in PhpDoc).

Illconditioned answered 2/5, 2011 at 14:4 Comment(4)
With your hints, I have fount that this is an open request in several PHP IDEs. The most common format is @return self and @return static although @return $this also appears frequently. I hope it will be implemented soon.Tetrad
Just a heads up if anyone's still searching: for IntelliJ IDEA, @return static works perfectly. It's even compatible with @return static[] when returning an array of objects.Maternity
The use of self, static and $this are documented in the PHPDoc manual, you can find them here: phpdoc.org/docs/latest/guides/types.html#keywordsSenarmontite
@Senarmontite Update link: docs.phpdoc.org/3.0/guide/guides/types.html#pseudo-typesInsulator
N
6

Updated answer taking as reference the most popular PHP IDE (PHPStorm 8):

For @return you can use:

  • self
  • $this

For @method you can use:

  • $this

Example:

/**
 * 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

Update for PHPStorm 10 (EAP)

It seems that now static can be used too, but only for @return.

Neil answered 29/7, 2014 at 13:14 Comment(0)
I
5

Updated cvsguimaraes' answer to include static options:

/**
 * 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
Ishtar answered 29/12, 2014 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.