How to document this?

For example, the following code:

abstract class A {
  public $strClassName;

  public function __construct($strClassName) { 
    $this->strClassName = $strClassName;
  }
    
  /**
   * @return mixed
   */
  public function get() {
    return new $this->strClassName();
   }
}


class B extends A {
  public function __construct() {
    parent::__construct("C");
  }
}


class C {
  public function hello() {
    return "Hello world!";
  }
}

$objB = new B();
$objC = $objB->get();
print $objC->hello(); // Output 'Hello world!'


The return form in the function get() is different in class B. In class A it is 'mixed', in class B it is 'C'. So the correct documentation should be:

/**
  * @return C
  */


But I don't overwrite that function in class B, so I have no place to put that documentation. As a result the AutoComplete doesn't know what the return form of B::get() is.
Is there a solution for this?

2 comments

Add "@method C get()" PHPDOC annotation (mind the more specific return type) to class B.

0

Please sign in to leave a comment.