2017-09-17 46 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; 
    } 
}