Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP数组元素声明为类对象_Php_Arrays_Types_Element_Declare - Fatal编程技术网

将PHP数组元素声明为类对象

将PHP数组元素声明为类对象,php,arrays,types,element,declare,Php,Arrays,Types,Element,Declare,我有一个数组: $arr = [ "banana" => new Fruit("banana"), "apple" => new Fruit("apple") ]; class Fruit { private $name; function __construct ($fruit_name) { $this->name = $fruit_name; } public function write() { echo $thi

我有一个数组:

$arr = [
   "banana" => new Fruit("banana"),
   "apple" => new Fruit("apple")
];

class Fruit {
  private $name;
  function __construct ($fruit_name) {
      $this->name = $fruit_name;
  }

  public function write() {
      echo $this->name;
  }
}

//Print
$arr["banana"]->write(); // result: banana
一切正常,但我的IDE(PHPStorm 9.0)警告上面的最后一行:找不到方法“write”。它无法识别数组元素的数据类型

如何将数组的元素声明为类对象,使IDE不会对此发出警告

谢谢大家!