Search code examples
phpphpstormphpdoc

PhpStorm won't recognize the methods of my object


I am writing some Object Oriented PHP code in PhpStorm and I've run into a problem.

I see that you need to define these PHPDoc comments and I'm trying to do the same. QuestionList is my "active" class, the MySQLAdapter is my other class which handles the database and SQL queries.

I am trying to define the constructors $sql_adapter parameter as a MySQLAdapter so that when I hit Ctrl + Space I can see my object's available functions but without any luck.

The first time I use my connect() method the IDE will autocomplete the method name, but after I initialize my sql field as $sql_adapter the IDE won't recognize my $sql object's methods.

What is the problem, am I not using the PHPDoc currently?

/**
 * @param QuestionList MySQLAdapter $sql_adapter
 */
public function __construct($sql_adapter){
    $this->questions = array();
    $this->sql = new MySQLAdapter();
    /* autocompletes this one */
    $this->sql->connect();
    $this->sql = $sql_adapter;
    /* won't autocomplete this one */
    $this->sql->connect();
}

Solution

  • @param QuestionList MySQLAdapter $sql_adapter is a nonsensical typehint. It's trying to tell the IDE that MySQLAdapter is of type QuestionList with the explanation of $sql_adapter. That obviously makes no sense. The annotation must be:

    @param MySQLAdapter $sql_adapter
    

    Better yet, use PHP's type hinting:

    public function __construct(MySQLAdapter $sql_adapter) ..