LoginSignup
1
1

More than 3 years have passed since last update.

ArrayAccessでコード補完(PhpStormネタ)

Posted at

ArrayAccessを使うとオブジェクトを配列のように使えます。

その際、いくつかメソッドを追加するのですが、返り値を指定することができます。
こんなコード。

    /**
     * @param mixed $offset
     * @return Hi
     */
    public function offsetGet($offset) {
        ....
    }

するとコード補完ができた。

スクリーンショット 2019-05-22 18.54.48.png

この「e()」が補完したいメソッド。

なお、$thisではだめだった。
PhpStormのバージョンは2019.1を利用。

コード

一応、クラスとコード。

class Hi implements ArrayAccess
{
    private $hi = '';
    public function e() {
        return $this->hi;
    }
    /**
     * @param mixed $offset
     * @return Hi
     */
    public function offsetGet($offset) {
        $this->hi .= $offset;
        return $this;
    }
    public function offsetSet($offset, $value) {}
    public function offsetUnset($offset) {}
    public function offsetExists($offset) {
        return false;
    }
}

$hi = new Hi();
echo $hi['h']['i']->e();
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1