Search code examples
phpvisual-studio-codeintelephense

VS Code PHP Intelephense objects in array not showing properties and functions to -> operator


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. :)


Solution

  • From the PHP documentation on arrays:

    The key [of an array] can either be an int or a string. The value can be of any type.

    So, for example, we can have:

    $obj1 = new MyClass;
    $my_array[0] = $obj1;
    

    But, since the values stored in an array can be of any type, there's nothing stopping us from then doing something like, say:

    $my_array[1] = "hello";
    $my_array[2] = 5;
    

    This makes it pretty difficult for an editor to infer that $my_array[0] contains a (pointer to) an object of type MyClass.

    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:

    screenshot showing suggestions

    So:

    <?php
    
    class MyClass
    {
        public $property1;
        public $property2;
    }
    
    $obj1 = new MyClass;
    $my_array[0] = $obj1;
    $obj1->property1 = "Random";
    $obj1->property2 = "Another";
    echo $my_array[0]->property1 . "<br>" . $my_array[0]->property2;
    
    

    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:

    /** @var MyClass[] */
    $my_array[0] = $obj1;
    

    then the editor can provide useful suggestions on the array too:

    code completion on array

    For more information, see this phpDocumentor page on DocBlocks.