在PHP ArrayAccess接口接口的好处? [英] Benefits of ArrayAccess Interface in PHP?

查看:107
本文介绍了在PHP ArrayAccess接口接口的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现了ArrayAccess接口在PHP中,我们可以访问对象的属性作为数组键。
什么是具有像一个数组的对象的行为所带来的好处?

Implementing ArrayAccess Interface in PHP , We can access Object Properties as Array Keys . What are the benefits of having an Object behave like an Array?

就像我看到实施形式与 ArrayAccess接口框架接口然后我们就可以访问(HTML)格式对象的字段类似,

Like I see Frameworks implementing 'FORM' with ArrayAccess Interface and then we can access (HTML) Form Objects Fields something like,

$form['nameField']   instead-of  $form->nameField 
$form['titleField']  instead-of  $form->titleField

请告诉我使用 $表['名称字段] INSTEAD-OF $形式 - >中受益;名称字段

Whats the benefit of using $form['nameField] instead-of $form->nameField

它是不是'数组数据结构或对象和数组形式之间的可移植性速度?

Is it Speed of 'Array Data Structures' or portability between Object and Array Forms ?

还是我失去了一些东西? :)

Or Am I missing something? :)

推荐答案

有没有功能性利益,而是结构性的。

There is no functional benefit, but structural.

如果你定义地图,你的建议,它可以包含命名的元素,类似于种任意数量。对象属性通常被明确定义和不同的类型。

If you define a map, you suggest, that it can contain an arbitrary number of named elements, that are of similar kinds. Object properties are usually well defined and of different types.

在短期:如果实施 ArrayAccess接口你说:我的目标(也)的行为就像一个数组

In short: If you implement ArrayAccess you say "My object (also) behaves like an array".

http://en.wikipedia.org/wiki/Associative_array

这篇关于在PHP ArrayAccess接口接口的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆