0

It was explained to me by a more senior PHP programmer that using PHP arrays is inefficient as it results in extra data copying. He then explained that you get less memory consumption and more performance by creating classes to hold your data, even arrays. PHP would then not feel compelled to copy/deep copy the data in a class as it would an array of arrays.

He also explained the object I would be looking for would be easier to find in a proper structured tree of objects than an array of arrays. This I can understand.

Here comes the questions, I know they're a little vague.

  • Is there ever a proper situation to use an array of arrays over an object to store data?

  • Will it always be more memory and performance efficient to store data in objects?

Thanks a lot in advance guys, I could write and profile a bunch of PHP apps to figure it out but I'm sure a lot of you already know the answer.

4
  • 2
    I assume that by "array" here you refer to associative arrays rather than those with sequential integer indices. Commented May 20, 2012 at 18:18
  • well I meant more arrays in general, I'm fairly new to programming and am not very sure of the performances differences between associative arrays and otherwise. Commented May 20, 2012 at 18:20
  • @Eric that assumption makes a huge difference. Commented May 20, 2012 at 18:20
  • 1
    Use what is most appropriate and easy to integrate into your application architecture. If your application is heavily OO, use objects, if you can make it work more easily with arrays, use arrays. Don't worry about memory consumption until your site fails to scale according to your needs. As a senior PHP dev myself, I can tell you that the minor memory differences between objects and arrays are usually the last thing on my mind. Commented May 20, 2012 at 18:21

3 Answers 3

1

This guy did a test and it seems there is not much difference : http://www.rooftopsolutions.nl/blog/148

Sign up to request clarification or add additional context in comments.

2 Comments

interesting, I figured it would be a much greater difference than that.
Why don't you run a test, must be pretty easy to set up.
1

Array and Class should not be mixed up. Class represents Real world entity and their action. You should not use array for this purpose. Associative array may supports the property but it does not support the actions.

The purpose of these three things trivial array with numeric indices, associative array and object are not same. Use what is better suite. Don't think about the performance right now. When the software is developed you should define use cases where you need performance improvement. Then start profiling.

Comments

1

The difference in memory consumption / deep-copying cames from the fact that by default arrays are passed to functions by value (hence, the array is being copied), and objects - by reference. Since you can pass array by reference as well (explicitly stating that the argument is by reference - function foo(& $bar)) there is no much difference in memory usage of array vs class. As other stated, you have to use array / object in the way it's semantically correct. PHP does not provide struct and using a class for holding structured data is often better then associative array, since you can declare the actual keys that are valid. But the reasons for choosing array vs class are no performance reasons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.