D
D
dim4ik2015-07-16 11:09:21
PHP
dim4ik, 2015-07-16 11:09:21

What is the advantage of ArrayObject over Array?

I just can’t understand what the difference is, and the main advantage of ArrayObject over Array.
The model that is requested for data returns an array of objects from the database.
Further already there is a work with an array of objects.
You can also return an ArrayObject, but why and for what I have no idea.
Please provide real examples of ArrayObject, where they are used, in order to sort out this issue once and for all.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-07-16
@27cm

The model that is requested for data returns an array of objects from the database.
Further already there is a work with an array of objects.

Take a situation where the array is very large (hundreds of thousands of objects) and it is not known in advance which part of the array will be needed, that is, it will work something like this:
foreach ($model->getArray() as $object) {
    // Делаем что-то с $object...
    if (!rand(0, 10)) {
        break;
    }
}

Doing it the way you suggest (via array) would have to create and return a full array of objects, most of which will not be used. Or change the code to something like:
foreach ($model->getIdsArray() as $id) {
    // Создаём $object по $id, запросив данные из хранилища
    // Делаем что-то с $object...
    if (!rand(0, 10)) {
        break;
    }
}

Using ArrayObject, you can easily implement the creation of only really used array objects and not duplicate the code for creating objects in each such loop.

A
Alexander Evgenievich, 2015-07-16
@banderos120

Inheritance, Encapsulation, Polymorphism.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question