【问题标题】:PHP 7 Arrays - Detection of property of an element of a 2D arrayPHP 7 Arrays - 检测二维数组元素的属性
【发布时间】: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 建议的属性列表中看到它?
  • 当然,完成。

标签: php arrays phpdoc


【解决方案1】:

如果您乐于使用 phpdoc 注释,您可以使用Type[][] 注释将变量声明为Type 的二维数组。在类属性的情况下,如下所示:

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

或者对于一个类方法的返回值:

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

对于 PHPStorm,它提供了这个:

【讨论】:

  • 谢谢!这听起来类似于我过去尝试完成的事情。我相信我以前可能已经过这座桥。我认为没有其他方法可以直接使用该语言来获取该属性吗?
  • @Vahe 不幸的是,据我所知,即使是像 PHPStorm 这样的顶级 IDE 也不会自行推断此信息,需要 phpdoc 的指导。
  • 由于某种原因,在我输入属性名称之前,我无法在箭头之后将属性显示为建议。我将 phpdoc 注释放在 offsetGet() 的顶部。
  • @Vahe 您的 IDE 可能对 phpdoc 的支持较低。我添加了 PHPStorm 如何处理注释的屏幕截图。
  • 我找到了这个,stackoverflow.com/questions/20543050/…,但我似乎无法让它与 PhpEd 一起工作
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 2013-06-10
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多