[英]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.我是 VS Code 的新手,所以我不知道这里发生了什么。 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.
我有一个全新的 Intelephense 安装,并禁用了基本的 PHP 建议,但是当我输入一个数组时,其中是对象,在我输入 -> 运算符后,object 属性和函数不会显示在建议中。
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.
问题是,当我输入 $array[$i] -> property1 = 'Random' 行时,它没有给出建议。
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.当我键入最后一行时,在键入 -> 运算符后,我希望 Intelephense 会列出 class newClass 的所有属性和函数。 But it doesn't.
但事实并非如此。
Also, I'm using normal array because objectArray doesn't support foreach according to official manual.另外,我使用的是普通数组,因为根据官方手册,objectArray 不支持 foreach。 Therefore, to me, is useless.
因此,对我来说,是无用的。
Thanks in advance.提前致谢。 :)
:)
From the PHP documentation on arrays :来自arrays 上的 PHP 文档:
The key [of an array] can either be an int or a string.
[数组的] 键可以是 int 或字符串。 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
.这使得编辑器很难推断
$my_array[0]
包含一个(指向) MyClass
类型的 object 的(指针)。
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->
.但是,由于您已经有
$obj1
指向这样的实例,因此在您输入$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.然后,由于
$obj1
和$my_array[0]
指向同一个 object ,因此通过$obj1
进行的任何编辑都会影响$my_array[0]
指向的 object 。
With only PHP Intelephense active, VS Code gives the following suggestions:只有PHP Intelephense处于活动状态,VS Code 给出以下建议:
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]
.将为我们提供这些设置,即使它们是通过 (pointer)
$obj1
设置并通过 (pointer) $my_array[0]
显示的。
Having said that, we can use DocBlock type hinting to let the editor know our intentions.话虽如此,我们可以使用DocBlock类型提示让编辑器知道我们的意图。 For example, if we have:
例如,如果我们有:
/** @var MyClass[] */
$my_array[0] = $obj1;
then the editor can provide useful suggestions on the array too:那么编辑器也可以对数组提供有用的建议:
For more information, see this phpDocumentor page on DocBlocks .有关更多信息,请参阅DocBlocks上的这个phpDocumentor页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.