Jonathan Mayhak
Jonathan Mayhak

Reputation: 12526

In phpDoc, how do I write a class I don't know yet will be returned?

The following is code for Base.php and the class Base

/**
 * @return ?
 */
public static function withId($id, $class = __CLASS__)
{
    $instance = new $class($id);
    if ($instance->getId() == self::ID_OF_UNKNOWN_USER) {
        $instance = null;
    }
    return $instance;
}

Other classes will extend this class. I'm using late static binding to figure out who should be created prior to calling withId() and passing the class name as $class.

The returned class could be Base or any of its children. How do I mark that in phpDoc?

Upvotes: 1

Views: 309

Answers (2)

rik
rik

Reputation: 8612

The returned class could be Base or any of its children. How do I mark that in phpDoc?

Straight forward.

/**
 * @return Base
 */

Upvotes: 0

abesto
abesto

Reputation: 2351

This looks somewhat like a factory pattern, in which case the user code shouldn't know the concrete class returned. Usually you'd use an abstract class or interface and program for that. In this case:

/**
 * @return null|Base
 */

There's no way to use values generated at runtime in docstrings (which are static).

Upvotes: 1

Related Questions