Advantages of the ArrayAccess interface in PHP?

Embedding the ArrayAccess interface in PHP. We can access object properties in the form of array keys. What are the benefits of having an object behave like an array?

As I see it, Frameworks implements "FORM" with an ArrayAccessinterface , and then we can access it (HTML). Objects form objects: / p>

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

What is the advantage of using $form['nameField]instead$form->nameField

Is the speed of "array data structures" or portability between forms of objects and arrays?

Or am I missing something? :)

+3
source share
3 answers

There are no functional advantages, but structural ones.

, , , . .

: ArrayAccess, : " () ".

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

+5

/ , (, ). , $form['some-field'] $form->{'some-field'}.

, __get, __set, __isset, __unset, ( ). . overloading.

+4

, . Iterator. , . , , - .

.

$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 create a usercollection object and restrict its list of users to the company identifier. Then, when I access the collection, I get users with this identifier from this company. The second part allows me to sort through each user in the company.

+1
source

Source: https://habr.com/ru/post/1777801/


All Articles