Using arrays or objects in yii2?

I have read the following article, https://steemit.com/php/@crell/php-never-type-hint-on-arrays which claims that we should abandon the use of arrays if we can, because they do not offer any advantage over classes. But in yii2 definite guide the following is mentioned, " While retrieving data in terms of Active Record objects is convenient and flexible, it is not always desirable when you have to bring back a large amount of data due to the big memory footprint. In this case, you can retrieve data using PHP arrays by calling asArray() before executing a query method:". What is the official approach from yii2 developers prespective?

That’s not at all what the author of that post refers to:

it often gets used as a cheap anonymous struct for complex data that the developer feels isn’t worth defining a class for.

He is referring to the practice of some PHP developers to use arrays as a kind of poor man’s class.

It explicitly says,

A class is twice as memory efficient as an array.

I just referred to the specific article because it demonstrates a good practise, also there is a relevant article from samdark imo. https://github.com/samdark/yii2-cookbook/blob/master/book/structure-collections.md.
The difference I think is in the size of the class, specifically active record, which is huge with all these methods, so probably this is the main reason. I have not yet profile an example to have feedback, I assume that probably someone else did. I will do it in the near future

You are referring to a class which has a helper function called ‘toArray’. That’s what it is.
Sometimes an array really is what you want. The choice is yours.
That the class has a member function that returns an array doesn’t make it less of a class.

Thank you for your reply jacmoe, but I probably have to rephrase my question. What I am asking is when to use class or array, not if I can or how. Specifically in the case of a lot of records from database. The yii recommends in this case to use arrays. Is it a good practise or not? Nevertheless this approach can be extended to many similar situations, as in php generally and in yii we have an extensive use of arrays as data structures.

Yii does not generally recommend that you use arrays, especially not if you are working with larger data sets. It is there if you want to use it, and the amount of data is small.
The choice is yours.

@giannis, if you are concerned about the efficiency and performance of an object compared to an array, you have to make sure that they are functionally equivalent.

An array returned by asArray() is not functionally equivalent to an array of ActiveRecord objects, since the latter provides much more features than the former. For example, an ActiveRecord object may have extra fields that are not in the database table. It may also have afterFind() method that calculates something each time the row is read from the database. It’s more convenient than a simple associative array, but consumes more time and memory. So comparing ActiveRecord object and an array returned by asArray() method makes little sense. If you have lots of records and you don’t need the convenience of ActiveRecord, then you want to use asArray(). That’s all.

In other words, what should be compared to an array returned by asArray() is not an array of ActiveRecord objects, but an array of some light weight objects return by an imaginary function called something like asLightObjectArray().

I’m a bit interested in the bench mark between them, but, well, I don’t think it will provide a good enough reason to make me give up using ARRAY which I think is one of the main reasons that made PHP so convenient to use.

1 Like