Recognizing Class

Lets say a Method returns an array of objects(instances) from class A. Now when i access an element of that array, is there a way to tell PHP/PhpStorm its an instance of class A, so i can use intellisense




$array = someClass->getArrayofObjects

foreach ($array as $element)
{
     echo element->getName()   //no intellisense
}

2 comments
Comment actions Permalink

Hi there,

Use PHPDoc comments, e.g.

/** @var myClass[] $array */
$array = someClass->getArrayofObjects();

or

$array = someClass->getArrayofObjects();

 
foreach ($array as $element)
{
    /** @var myClass $element */

    echo element->getName();
}

or even better -- add proper doc for that actual method, e.g.

/**
* My super useful method
*
* @return myClass[]
*/
public function getArrayOfObjects()
{
  // method body

}

0
Comment actions Permalink

thanks - proper phpdoc works for me ;)

0

Please sign in to leave a comment.