PHP hint dynamic return type (based on $ class parameter)

Is it possible that you are hinting at the return type of a PHP function / method being defined by one, if those are arguments? Something like that:

/**
 * @param string $class
 * @param array $attributes
 * @return $class <- this doesn't work
 */
public function create($class, $attributes) {
    ... // finally returns object of type $class
}

      

I am implementing a library that allows you to create factory objects for unit tests. Therefore, I want it to be possible in the client code:

$user = $factory->create('Users', ['name' => 'John', 'email' => 'john@example.com']);

      

And have autocomplete object methods / attributes without having to add explicit PHPDoc comments to client code like this:

/* @var $user Users */
$user = $factory->create('Users', ['name' => 'John', 'email' => 'john@example.com']);

      

Any suggestions are greatly appreciated! While I'm working on popular IDEs (PHPStorm, Eclipse, NetBeans) this is a valid solution.

+3


source to share





All Articles