PHP 7 个数组 - 检测 属性 二维数组的元素
PHP 7 Arrays - Detection of property of an element of a 2D array
我希望我的 PHP IDE (NuSphere PhpEd) 检测我的 2D 数组元素(对象)的 属性,其 属性 在之后未显示我在 IDE.
中输入右箭头
PHP7 中有什么方法可以自动生成多维数组元素属性的建议,其中每个元素都是具有特定属性的对象?
<?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 注释,可以使用 Type[][]
注释将变量声明为 Type
的二维数组。在 class 属性 的情况下,看起来像:
/** @var Cell[][] */
private $matrix = [];
或者对于 class 方法的 return 值:
/**
* @return Cell[][]
*/
public function getMatrix() {
return $this->matrix;
}
对于 PHPStorm,它提供了:
我尝试了一种变通方法,以强制 phpdoc 按照箭头选择我的 属性 或方法。
这是我做的。
class Matrix
{
protected $maxX;
protected $maxY;
private $matrix = array();
public function __construct($maxX, $maxY)
{
$this->maxX = $maxX;
$this->maxY = $maxY;
$this->matrix = array_fill(1, $maxX, array_fill(1, $maxY, 0));
return $this;
}
public function getMaxX()
{
return $this->maxX;
}
public function getMaxY()
{
return $this->maxY;
}
public function get($x, $y)
{
if (isset($this->matrix[$x][$y]))
return $this->matrix[$x][$y];
throw new OutOfBoundsException("Array at indices $x, $y is out of bounds!");
}
}
class Main
{
public function __construct()
{
}
public function setArrayVal($x, $y)
{
$cell = new Cell();
//Set Value in some arbitrary method in Main Class
$this->matrix($x, $y)->set($cell);
//Or if $val is public in Cell class
// $this->matrix($x, $y)->val = $cell;
}
//Method of Main Class
public function matrix($x, $y) : Cell //Note Cell here specified for type hinting
{
//Note, matrix below is a property, not method, whose type corresponds to the matrix class
return $this->matrix->set($x, $y);
}
}
class Cell
{
private $val;
public function __construct()
{
}
// Method set in Cell class
public function set($val)
{
$this->val = $val;
}
}
我希望我的 PHP IDE (NuSphere PhpEd) 检测我的 2D 数组元素(对象)的 属性,其 属性 在之后未显示我在 IDE.
中输入右箭头PHP7 中有什么方法可以自动生成多维数组元素属性的建议,其中每个元素都是具有特定属性的对象?
<?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 注释,可以使用 Type[][]
注释将变量声明为 Type
的二维数组。在 class 属性 的情况下,看起来像:
/** @var Cell[][] */
private $matrix = [];
或者对于 class 方法的 return 值:
/**
* @return Cell[][]
*/
public function getMatrix() {
return $this->matrix;
}
对于 PHPStorm,它提供了:
我尝试了一种变通方法,以强制 phpdoc 按照箭头选择我的 属性 或方法。
这是我做的。
class Matrix
{
protected $maxX;
protected $maxY;
private $matrix = array();
public function __construct($maxX, $maxY)
{
$this->maxX = $maxX;
$this->maxY = $maxY;
$this->matrix = array_fill(1, $maxX, array_fill(1, $maxY, 0));
return $this;
}
public function getMaxX()
{
return $this->maxX;
}
public function getMaxY()
{
return $this->maxY;
}
public function get($x, $y)
{
if (isset($this->matrix[$x][$y]))
return $this->matrix[$x][$y];
throw new OutOfBoundsException("Array at indices $x, $y is out of bounds!");
}
}
class Main
{
public function __construct()
{
}
public function setArrayVal($x, $y)
{
$cell = new Cell();
//Set Value in some arbitrary method in Main Class
$this->matrix($x, $y)->set($cell);
//Or if $val is public in Cell class
// $this->matrix($x, $y)->val = $cell;
}
//Method of Main Class
public function matrix($x, $y) : Cell //Note Cell here specified for type hinting
{
//Note, matrix below is a property, not method, whose type corresponds to the matrix class
return $this->matrix->set($x, $y);
}
}
class Cell
{
private $val;
public function __construct()
{
}
// Method set in Cell class
public function set($val)
{
$this->val = $val;
}
}