コード例 #1
0
ファイル: 004.php プロジェクト: badlamer/hhvm
    {
        $this->data = $data;
    }
    public function getArrayAccess()
    {
        /* create a proxy object implementing array access */
        return new class($this->data) extends Outer implements ArrayAccess
        {
            public function offsetGet($offset)
            {
                return $this->data[$offset];
            }
            public function offsetSet($offset, $data)
            {
                return $this->data[$offset] = $data;
            }
            public function offsetUnset($offset)
            {
                unset($this->data[$offset]);
            }
            public function offsetExists($offset)
            {
                return isset($this->data[$offset]);
            }
        };
    }
}
$outer = new Outer(array(rand(1, 100)));
/* not null because inheritance */
var_dump($outer->getArrayAccess()[0]);
コード例 #2
0
ファイル: 005.php プロジェクト: badlamer/hhvm
    }
    public function getArrayAccess()
    {
        /* create a child object implementing array access */
        /* this grants you access to protected methods and members */
        return new class($this->data) implements ArrayAccess
        {
            public function offsetGet($offset)
            {
                return $this->data[$offset];
            }
            public function offsetSet($offset, $data)
            {
                return $this->data[$offset] = $data;
            }
            public function offsetUnset($offset)
            {
                unset($this->data[$offset]);
            }
            public function offsetExists($offset)
            {
                return isset($this->data[$offset]);
            }
        };
    }
}
$data = array(rand(1, 100), rand(2, 200));
$outer = new Outer($data);
$proxy = $outer->getArrayAccess();
/* null because no inheritance, so no access to protected member */
var_dump(@$outer->getArrayAccess()[0]);