Reputation: 1272
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();
}
Upvotes: 1
Views: 945
Reputation: 29932
Yes, you can use PHPDocs to have this working correctly.
For example the object member $sql
should be declared and documented in the class:
<?php
class Test {
/**
* @var MySQLAdapater
*/
private $sql;
/**
* @param MySQLAdapter $sqlAdapter [description]
*/
public function __construct(MySQLAdapter $sqlAdapter) {
$sqlAdapter; // …is recognized as type MySQLAdapter through @param doc-comment
$this->sql; // …is recognized as type MySQLAdapter through @var doc-comment
}
}
now the IDE recognizes any uses of $this->sql
as the type MySQLAdapter
.
Example updated based on the comment from @ashnazg.
Upvotes: 3
Reputation: 522606
@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) ..
Upvotes: 4