简体   繁体   English

PHP中ArrayAccess接口的好处?

[英]Benefits of ArrayAccess Interface in PHP?

Implementing ArrayAccess Interface in PHP , We can access Object Properties as Array Keys . 在PHP中实现ArrayAccess接口,我们可以将对象属性作为数组键访问。 What are the benefits of having an Object behave like an Array? 使Object的行为类似于数组有什么好处?

Like I see Frameworks implementing 'FORM' with ArrayAccess Interface and then we can access (HTML) Form Objects Fields something like, 就像我看到Frameworks使用ArrayAccess接口实现'FORM'然后我们可以访问(HTML)表单对象字段之类的,

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

Whats the benefit of using $form['nameField] instead-of $form->nameField 使用$form['nameField]而不是$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. 对象属性通常定义良好且类型不同。

In short: If you implement ArrayAccess you say "My object (also) behaves like an array". 简而言之:如果您实现ArrayAccess您会说“我的对象(也)就像一个数组”。

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

Well for a start it makes it easier/more natural to access members whose key does not form a valid identifier (eg contains awkward characters). 好一开始,它可以更容易/更自然地访问其密钥不形成有效标识符的成员(例如包含笨拙的字符)。 For example, you can write $form['some-field'] instead of the more cumbersome $form->{'some-field'} . 例如,您可以编写$form['some-field']而不是更麻烦的$form->{'some-field'}

Other than this, it's essentially the same as implementing the __get , __set , __isset , __unset magic methods, except allowing a different syntax (and with slightly different semantic connotations). 除此之外,它与实现__get__set__isset__unset魔术方法基本相同,只是允许使用不同的语法(并且语义内容略有不同)。 See overloading . 看到重载

Using the Interfaces make more powerful tools, easier to use. 使用Interfaces可以制作更强大的工具,更易于使用。 I use it for collections, along with Iterator. 我将它与Iterator一起用于收藏。 It allows your object to act like an array even when it isn't. 它允许您的对象即使不是数组也像数组一样。 The power comes from not to map property name to an array element, but having the array access do something completely different. 权力来自于不将属性名称映射到数组元素,但是使用数组访问可以完全不同。

ie

$users = new UserCollection();
$users->company = $companyid;
$user = $users[$userid];

// also
foreach( $users as $user ) {
  // Do something for each user in the system
}

In the first part, I'm creating a usercollection's object, and restricting its users list to that of a company id. 在第一部分中,我正在创建一个usercollection的对象,并将其用户列表限制为公司ID。 Then when I access the collection, I get users with that id from that company. 然后当我访问该集合时,我从该公司获得具有该id的用户。 The second parts allows me to iterate over each user in a company. 第二部分允许我迭代公司中的每个用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM