Flight框架如何避免未定义的索引错误


How does Flight framework avoid undefined index error

我想知道,Flight框架如何在运行时避免未定义的索引错误?

例如,

$result = Flight::request()->query['no_such_key'];

将仅返回CCD_ 1,其中CCD_。

但是,如果我使用自己的数组,我会得到未定义的索引

$result = $array['no_such_key'];

我可以知道飞行使用的是什么技术吗?

Flight::request()->query不是数组,它是实现arrayAccess接口的对象。

例如

class Dummy implements ArrayAccess {
    public function offsetExists ( $offset ){}
    public function offsetGet ( $offset ){return $offset;}
    public function offsetSet ( $offset, $value ){}
    public function offsetUnset ( $offset ){}
}
$dummy = new Dummy();
echo $dummy['any_index']; // produce 'any_index' without any errors

查看以下代码:https://github.com/mikecao/flight/blob/master/flight/util/Collection.php,它实现了ArrayAccess接口。