2

I'm currently working on a project which requires me to only output objects when querying database and/or processing data. I have been hearing from other senior developers that, objects are "cheap" in term of memory and, I "kind of" agree with that.

So, my questions:

  • If I have an array as result of a query. Why not use the array "as is" since it already exists?
  • Is it really a good practice (besides standardization) to convert it into an object?
  • Does it really increase performance? (I can't grasp it because by converting the result into an object, I'm basically creating another entity, besides the already existing array, containing the same data).
Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81
Jay
  • 312
  • 3
  • 12

2 Answers2

0

In my opinion, I would say that you should NOT convert them into objects. You already have them as arrays, so transforming/copying them into object would require more memory as at a given time, you end up with both, the objects and arrays. If you absolutely do not need any OOP functionality (inheritance, private proprieties/methods, ...) you should stick with your arrays.

But keep in mind: You can often fetch the database and get objects instead of arrays as result. (See PDO fetchAll) Then you already have your objects without having the array.

If you wonder how these both parties work in terms of performance, have a look at Using arrays VS objects for storing data.

From what I understand, (but I might as well be wrong) PHP arrays are not arrays compared to the classical C arrays. A PHP array is a sort of object, since you have freedoms to i.e. change an array size (simply add a value). So you do not have the exact same performance as you would have with C arrays.

Conclusion: Feel free to keep your arrays. If you do not need the advantages of objects, no need to use ressources to convert them. After all, it's also a question of programming style, best practices and project guidelines.

Community
  • 1
  • 1
Lozik
  • 149
  • 1
  • 9
  • I completely agree with you, I don't see the benefit of converting arrays if the object features are not to be exploited. Our team is currently refactoring a huge project which, database connector layer has yet to be changed and, it output arrays only. As such, I brought the argument that, converting arrays wouldn't increase performance since we would just be duplicating data but still, there were enough seniors telling me otherwise so, I start guessing myself if my thinking was really wrong. Thanks for the overview! – Jay Oct 01 '16 at 13:03
0

It depends on what next is the code doing with the array.

If you leave the data in array you should be careful where you pass it next, because you do not want that array go all over the code base back and forth.

With array you cannot define any definable interface between classes or logic code blocks and that is not very predictable.

When you have array passed through 10 methods in 5 classes modifying the contents of that array its very hard to track what and where is happening to the contents.

I like this answer: https://www.reddit.com/r/PHP/comments/29eope/stop_abusing_arrays_in_php/cik8tet/

Ales
  • 381
  • 6
  • 8