百科狗-知识改变命运!

ArrayObject::offsetGet() - 各种类及接口

百变鹏仔2年前 (2023-11-21)阅读数 46#技术干货
文章标签范例

ArrayObject::offsetGet()

(PHP 5, PHP 7)

Returns the value at the specified index

ArrayObject::offsetGet() - 各种类及接口

说明

publicArrayObject::offsetGet(mixed $index): mixed

参数

$index

The index with the value.

返回值

The value at the specified index orNULL.

错误/异常

Produces anE_NOTICEerror message when the specified index does not exist.

范例

ArrayObject::offsetGet()example


以上例程会输出:

int(7)
string(4) "e.g."
bool(false)
It's worth noting that the PHP devs *did* fix the issue brought up in Sam's comment in 2013 (per the comment thread in his link here: https://bugs.php.net/bug.php?id=34783#1366088374), since at least PHP 5.3.8.
So, you *can* (and in most cases should) overload offsetGet with reference syntax to get expected functionality:

If you're overloading ArrayObject, it's worth noting that while this method (when implemented by the parent) will return a reference, so code like $fakeArray['foobar']['hello'] = 1; will work like you expect.
However, when you overload the offsetGet method, you CANNOT define it as &offsetGet, so the above code falls out (because it returns the 'foobar' variable before you actually work with it).
This is something that the developers broke between 5.0 and 5.1, and was closed as bogus (http://bugs.php.net/bug.php?id=34783). So this is not a big, or question, or request, but just something worth noting.
Speaking of offsetGet() method overloading, be advised, that if you're iterating through Object via foreach, this method wouldn't be called. Iterator's current() method will be called instead.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}