【发布时间】:2017-09-17 21:46:51
【问题描述】:
我希望我的 PHP IDE (NuSphere PhpEd) 检测我的 2D 数组元素(对象)的属性,当我在 IDE 中键入右箭头后,该属性未显示。
PHP 7 中有什么方法可以自动生成多维数组元素属性的建议,其中每个元素都是具有某些属性的对象?
<?php
class Cell
{
private $color;
public function __construct()
{
$this->color = "red";
}
public function __get($propertyName)
{
if ($propertyName == 'color')
return $this->color;
}
public function __set($propertyName, $value)
{
if ($propertyName == 'color')
$this->color = $value;
}
}
class Matrix implements ArrayAccess
{
private $matrix = array();
public function __construct($maxX, $maxY)
{
$this->matrix = array_fill(1, $maxX, array_fill(1, $maxY, null));
}
public function &offsetGet($name)
{
return $this->matrix[$name];
}
public function offsetSet($name, $value)
{
$this->matrix[$name] = $value;
}
public function offsetExists($name)
{
return isset($this->matrix[$name]);
}
public function offsetUnset($name)
{
unset($this->matrix[$name]);
}
}
$matrix = new Matrix(3,3);
for ($xIdx = 1; $xIdx <= 3; $xIdx++)
for ($yIdx = 1; $yIdx <= 3; $yIdx++)
$matrix[$xIdx][$yIdx] = new Cell();
$matrix[2][2]->color = "green";
echo $matrix[2][2]->color;
?>
【问题讨论】:
-
您正在进入 phpdoc 领域,这是一种允许您提供此类信息的技术。
-
感谢您的回复,您能否有一个示例说明当我键入箭头时如何在 phpdoc 建议的属性列表中看到它?
-
当然,完成。