PhpStorm - Passing an object to another class gives me a warning when I try to use the Methods (reference method not found).

I know I can suppress the warnings and so on, But i love the suggestion feature of phpStorm 2016.
Is there a way to get phpStorm to go deeper? a plugin or something? Am I missing a setting? Am pretty new to phpStorm and PHP too


phpStorm produces tips/suggestions for the objects taObj and tbObj as shown in this picture



But when the object is passed as a  parm, no tips/suggestions are displayed and I get "reference method is not found warning"


4 comments
Comment actions Permalink

 

RAW CODE:
class TableA{
private $var = "a";
function getVar(){
return $this->var;
}
public function __construct(){
//nothing
}
}
class TableB{
private $var = "b";
function getVar(){
return $this->var;
}
public function __construct($xxObj){
$xxObj->getVar(); //displays no suggestions
echo $xxObj->getVar(); //displays no suggestions
}
};



$taObj = new TableA;
$tbObj = new TableB($taObj);//Outputs "a"
echo $taObj->getVar(); //Outputs "a" //displays suggestions tips
echo $tbObj->getVar(); //Outputs "b" //displays suggestions tips

 

0
Comment actions Permalink

Hi there,

Use proper type hint -- PHP's own type hinting will work just fine and preferred here (as that parameter is an object)

    public function __construct(TableA $xxObj){
        $xxObj->getVar();        //displays no suggestions
        echo $xxObj->getVar();   //displays no suggestions
    }

Otherwise -- you may use PHPDoc comments for type hinting: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#722-var

    /**
     * TableB constructor.
     *
     * @param TableA $xxObj
     */
    public function __construct($xxObj){
        $xxObj->getVar();        //displays no suggestions
        echo $xxObj->getVar();   //displays no suggestions
    }

 

 

 

 

1
Comment actions Permalink

Thanks, That worked great.
But suppose I use an array as my param. Script still outputs the exact same info, but it broke some of the tip displays.
I can not or do not know how to use the type hint on the array that is mixed, it contains the an object and a field.
The phpDoc type hinting did not work either

...
...
public function __construct(array $options=array()) {

$xxObj->getVarA(); //displays no suggestions
echo $xxObj->getVarA(); //displays no suggestions
echo $foo; //outputs 99
}
}
$taObj = new TableA;
$tbObj = new TableB(array('xxObj'=>$taObj,'foo'=>99));
...
...

 

0
Comment actions Permalink

1) Your code sample (function body) makes little sense -- you are using $xxObj variable that comes from nowhere.

2) In any case -- AFAIK PhpStorm does not provide completion for individual array elements, especially if they are of the different type. But you can try Options completion plugin -- accordingly to the description it does just that (with the help of proper PHPDoc comment, of course)

0

Please sign in to leave a comment.