2

Given the following code structure is there a way that I can get the return type of FooFactory->createService method with PHP 5.6? I've tried ReflectionClass and ReflectionMethod classes but couldn't find a way.Thanks in advance.

class FooFactory implements FactoryInterface {
    public function createService(/*args*/)
    {
        $foo = new Foo();
        // ...
        //inject dependencies
        // ...
        return $foo;
    }
}

class Foo {
    /*...methods...*/
}

Edit: I need to get the class name without creating an instance of the FooFactory.

2

1 Answer 1

2

Using PHP5 this is not possible.

You could (and this is more of a workaround than a solution) type hint your variable on declaration. This would expose the methods etc belonging to created service to your IDE.

Example:

/* @var \MyFooService $myFooService */
$myFooService = FooFactory->createServixce('MyFooService');

Note it is the @var declaration that will inform your editor of the variable type.

Syntax being:

/* @var [CLASS_NAME] [VARIABLE] */

UPDATE:

Just seen you dont want to create an instance of FooFactory. By default arn't factory methods static?

So:

$fooFactory = new FooFactory();
$fooService = FooFactory->getService('FooService');

Becomes:

$fooService = FooFactory::getService('FooService');

Not the answer you're looking for? Browse other questions tagged or ask your own question.