ArrayAccess::offsetGet
(PHP 5, PHP 7, PHP 8)
ArrayAccess::offsetGet — 获取一个偏移位置的值
参数
-
offset
-
需要获取的偏移位置。
返回值
可返回任何类型。
注释
注意:
此方法的实现可以通过引用返回。 这使得可以间接修改 ArrayAccess 对象,能够重载数组的维度。
直接修改是完全替代数组维度的值,例如
$obj[6] = 7
。 另一方面,间接修改是指仅修改某个维度中的一部分,或者传引用的方式赋值一个维度, 例如$obj[6][7] = 7
和$var =& $obj[6]
。 使用++
自增或者使用--
自减也是通过间接修改的方式实现的。直接修改会触发对 ArrayAccess::offsetSet() 的调用,而间接修改则会触发对 ArrayAccess::offsetGet() 的调用。在这种情况下, ArrayAccess::offsetGet() 的实现必须能通过引用返回,否则会引发
E_NOTICE
消息。

User Contributed Notes 1 note
Martin Q ¶
2 years ago
As of PHP 7, offsetExists($offset) must return a TRUE value in order for offsetGet($offset) to be called, otherwise offsetGet($offset) will just return NULL. This was not the case in PHP 5, so if your code suddenly stops working upon upgrade to PHP 7, make sure offsetExists() returns sensible values.
备份地址:http://www.lvesu.com/blog/php/arrayaccess.offsetget.php