Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

factory - PhpStorm type-hinting for factories?

I have code more or less like this:

class Foo {
    public static function factory($str) {
        $class = "Foo_" . $str;
        return new $class;
    }
}
class Foo_Bar {
    public function razzle() {
        print "Foo_Bar->baz() was called";
    }
}

$Obj = Foo::factory('Bar');

and I would like PhpStorm to understand that $Obj is a Foo_Bar object, so that for example if I type $Obj->raz, razzle() will show up for autocompletion.

Is there any way to get this? To tell PhpStorm that the function Foo::factory($str) returns an object of type Foo_$str? My guess is that the answer is no.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I know you can't make PHPStorm understand what Foo is giving out, because it is dynamic. However you can surely tell PHPStorm what your $Obj variable is, by putting a doc comment above it, like so:

/** @var Foo_Bar $Obj */
$Obj = Foo::factory('Bar');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...