2017-09-17 13 views
0

내 PHP IDE (NuSphere PhpEd)에서 오른쪽에 입력 한 속성이 표시되지 않는 2D 배열 요소 (개체)의 속성을 검색하고 싶습니다. 내 IDE의 화살표.PHP 7 배열 - 2D 배열 요소의 속성 감지

PHP 7에서 자동으로 각 요소가 특정 속성을 가진 객체 인 다차원 배열 요소 속성 제안을 생성하는 방법이 있습니까? 당신이하는 PHPDoc 주석을 사용하는 것이 행복 경우

<?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

, 당신은 Type의 2 차원 배열로서 변수를 선언 할 Type[][] 주석을 사용할 수 있습니다. 보이는 클래스 속성의 경우와 같은 :

enter image description here이 제공 PHPStorm의 경우

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

:

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

또는 클래스 메소드의 반환 값

+0

감사합니다! 이것은 내가 과거에 성취하려고 시도했던 것과 비슷하게 들립니다. 나는 내가이 다리를 건너 았을지도 모른다라고 생각한다. 나는 언어로 직접 재산을 나가는 것과 같은 다른 방법이 없다고 생각합니까? – Vahe

+0

@Vahe 불행히도 필자가 볼 수 있듯이 PHPStorm과 같은 최고급 IDE조차도이 정보를 스스로 유추하지 않으며 phpdoc의 지침을 필요로합니다. – Marty

+0

어떤 이유로 나는 속성 이름을 입력하기 전에 속성을 화살표 뒤에 제안으로 표시 할 수 없습니다. 나는 offsetGet() 위에 phpdoc 어노테이션을 놓았다. – 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; 
    } 
}