1

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

Shadow0013
  • 61
  • 7

1 Answers1

1

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.

Richard Ambler
  • 4,816
  • 2
  • 21
  • 38
  • Yes, it SHOULD be able to suggest this, but it doesn't. Just how do I make it so that Intelephense extension does give me those suggestions? :D – Shadow0013 May 08 '21 at 14:23
  • Strange — it works on my end. I've added a screenshot to my post so you can see what I get. I don't remember fiddling around with the default settings too much. – Richard Ambler May 08 '21 at 23:27
  • Oh that. Well that does nothing, because I have an array of objects, and the original objects are long gone, since they were set using a for loop, and they were unset as the last line in the for loop. So ofc it gives recommendations for individual and isolated objects, but the problem is, that referring to them as $my_array[0]-> doesn't. So I updated my original post to reflect what I mean even better. – Shadow0013 May 09 '21 at 07:02
  • Dear Lord! I see your edit and raise you one edit! – Richard Ambler May 09 '21 at 07:58
  • Ahh okay, made it work. Thank you very much, I had no idea about this hinting. These editor phrases like hinting and linting meant nothing to me, and now that I googled it, I still can't really find a proper source for reading about it, for syntax, but with that line @var line, it works. So for the future, can you tell me what the syntax is for any type of variable? Or link a page where it's properly explained pls? :) – Shadow0013 May 09 '21 at 08:27
  • No problem. :) I've added a link to the post. Enjoy! – Richard Ambler May 09 '21 at 09:07