Home » Php » php – code completion and factory pattern in eclipse pdt

php – code completion and factory pattern in eclipse pdt

Posted by: admin July 12, 2020 Leave a comment

Questions:

Lets say I have a typical factory pattern in PHP code:

abstract class Model
{
    function m()
    {
    }
}

class model_A
{
    function a()
    {
    }
}

class model_B
{
    function b()
    {
    }
}

function modelFactory($name)
{
    $className = 'model_' . $name;
    $object = new $className();
    // ... do some magic stuff ...
    return $object;
}

I know about the @var and @return phpdoc tags, but is there any magic way so after typing this:

$x = modelFactory('A');

Eclipse will know $x is an instance of model_A?

Can I define somewhere a fixed vector of strings like this:

"modelFactory('A')" => "new model_A()"
"modelFactory('B')" => "new model_B()"

For Eclipse to replace in memory before processing with code completion.

How to&Answers:

Short answer, this is not possible in the way you want it.

As you’ve already rightfully pointed out in another comment you should use @var manually to achieve code completion goodness, because it’s impossible for the editor to understand the concept of programming patterns by itself

Although theoretically possible, manually authoring rules for type inference just seems backwards imho (let alone the required changes to the Docblock notation itself).

Answer:

The only IDE that supports this is PHPStorm (via a .phpstorm.meta.php file).

I filed a feature request asking to support this.
You can vote for it at https://bugs.eclipse.org/bugs/show_bug.cgi?id=463110

Answer:

i use eclipse and pdt from almost 3+ years,but dont have perfect answer for your question

i think Zend Studio 9 can solve your problem,for code completion it is far more better than pdt.

http://www.zend.com/en/products/studio/downloads

Answer:

Currently, I’m not aware of an IDE/Editor that does this either, but I’m keeping track of the same functionality for PhpStorm (they’re currently ‘working’ on it):
http://youtrack.jetbrains.com/issue/WI-6027