2017-09-17 50 views
0

我希望我的PHP IDE(NuSphere PhpEd)能够检测到我的二维数组元素(一个对象)的属性,该属性在我键入右后没有显示出来箭头在我的IDE。PHP 7数组 - 检测二维数组元素的属性

有什么办法在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; 
?> 
+0

您正在进入phpdoc的领域,这是一种技术,它允许您提供该类型的信息。 – Marty

+0

感谢您的回复,您是否偶然会有一个例子,说明我在输入箭头时如何看到在phpdoc建议的属性列表中? – Vahe

+1

当然,完成了。 – Marty

回答

2

如果你乐于使用PHPDoc的注释,你可以使用Type[][]注解来声明一个变量作为是Type二维数组。在一个类的属性,看起来的情况下,如:

/** @var Cell[][] */ 
private $matrix = []; 

或者一类方法的返回值:

/** 
* @return Cell[][] 
*/ 
public function getMatrix() { 
    return $this->matrix; 
} 

在PHPStorm的情况下,提供这样的:

enter image description here

+0

谢谢!这听起来与我过去尝试完成的事情类似。我相信我可能以前穿过这座桥。我认为没有其他方式可以直接用语言来获得房产? – Vahe

+0

@Vahe不幸的是,从我所看到的情况来看,即使像PHPStorm这样的顶级IDE也不会自己推断出这些信息,并且需要phpdoc的指导。 – Marty

+0

出于某种原因,我无法在将箭头键入属性名称之前将属性显示为建议。我将phpdoc注释放置在offsetGet()的顶部。 – Vahe

0

我试着解决方法,以强制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; 
    } 
}