I am new to VS Code, so I don't know what's happening here. I have a fresh Intelephense installation, and have disabled basic PHP suggestions, but when I type out an array, in which are objects, after I type the -> operator, the object properties and functions don't show up in the recommendations.
Is there a way to make it work?
Example:
class newClass {
public $property1;
public $property2;
public function NewFunction {
return 'Something';
}
}
$array = array();
for($i = 0; $i < 2; $i++ {
$temp = new newClass;
$temp -> property2 = 'SomeData';
$array[$i] = $temp;
$array[$i] -> property1 = 'Random';
unset($temp);
}
EDIT: So this is how I read in an array full of objects. Problem is, when I type in the $array[$i] -> property1 = 'Random' line, it doesn't give the recommendation.
When I type the last row, after I type the -> operator, I would expect Intelephense to list all the properties and functions of class newClass. But it doesn't.
Also, I'm using normal array because objectArray doesn't support foreach according to official manual. Therefore, to me, is useless.
Thanks in advance. :)
From the PHP documentation on arrays:
So, for example, we can have:
But, since the values stored in an array can be of any type, there's nothing stopping us from then doing something like, say:
This makes it pretty difficult for an editor to infer that
$my_array[0]
contains a (pointer to) an object of typeMyClass
.However, since you already have
$obj1
pointing to such an instance, the editor should be able to give pretty good suggestions after you've typed in$obj1->
. Then, since$obj1
and$my_array[0]
point to the same object, any edits made via$obj1
would affect the object$my_array[0]
is pointing to.With only PHP Intelephense active, VS Code gives the following suggestions:
So:
will give us these settings, even though they were set via (pointer)
$obj1
and displayed via (pointer)$my_array[0]
.Having said that, we can use DocBlock type hinting to let the editor know our intentions. For example, if we have:
then the editor can provide useful suggestions on the array too:
For more information, see this phpDocumentor page on DocBlocks.