28

Possible Duplicate:
When should I use stdClass and when should I use an array in php5 oo code ??

What are the benefits of using one of the two structures over the other?

// array
$user['name'] = 'Emanuil';

// object
$user->name = 'Emanuil';
Community
  • 1
  • 1
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • 2
    Why do you say 1 is better than 2? I personally do not agree with that statement. Can you explain why you think 1 is better than 2? Are we talking about efficiency, memory consumption, coding practice? Have a look at: http://particletree.com/notebook/object-oriented-php-memory-concerns/ also. – Chris Sep 14 '10 at 14:03
  • Just a tip: maybe arrays are consuming less resources. – fabrik Sep 14 '10 at 14:03
  • @Chris: I think I read somewhere that if you need a structure to just store data then 1 is the preferred way to go. – Emanuil Rusev Sep 14 '10 at 14:06
  • @fabrik: You are right. I shouldn't have asked for the "better". I edited the question to address that. – Emanuil Rusev Sep 14 '10 at 14:09
  • Also remotely related (very specialized): http://stackoverflow.com/questions/3218582/php-array-of-arrays-vs-array-of-objects – Pekka Sep 14 '10 at 14:11
  • @Pekka yeah, I'd say that too if it was my answer that got accepted in the duplicate ;) Although it should be noted that the linked dup asks for the use of Arrays and StdClass when returning values, while Emanuil asked in general. – Gordon Sep 14 '10 at 14:12
  • @Gordon and @Pekka: Thanks for the link! Answers my question. Apologies for the duplicate. – Emanuil Rusev Sep 14 '10 at 14:14
  • @Gordon yeah, I pasted the link because of your answer really – Pekka Sep 14 '10 at 14:18
  • 1
    This was handy for me, http://stackoverflow.com/questions/2048189/objects-versus-arrays – David Yell Sep 14 '10 at 14:34

3 Answers3

44

Arrays

  • There are tons of array_* functions that can work on arrays, most of which are very fast.
  • By default they passes by value (copied around)
  • Lightweight/Simple (changes only effect local variable, less to think about)
  • Often used for build once data (data that doesn't change)
  • All data is public
  • Slightly less resource intensive

Objects

  • Methods can be used to keep the data stricter. (IE. checks that a field fits a format)
  • Subclassing (reducing code duplication)
  • By default they are passed by reference
  • Changes to data can have cascading effects (__get, __set, etc)
  • Often used for data that is more mutable
  • Can protect data from outside via functions and protected/private variables
  • Function type hinting of objects is more flexible (different typehint for different classes)
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
13

Just run a simple test:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    new stdClass();
}
$total_object = microtime(true) - $ts_o;

Versus:

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    array();
}
$total_array = microtime(true) - $ts_a;

And calculate the he results.

echo 'Object: ' . $total_object . ' / Array: ' . $total_array;

Results: Object: 0.002635 / Array: 0.001243

As you can see that Arrays are faster in regards to speed, average 46.6% infact.

But when you start adding variables they suddenly turn around:

$ts_o = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = new stdClass();
    $var->booleon = true;
}
$total_object = microtime(true) - $ts_o;
unset($var);

$ts_a = microtime(true);
for($i=0;$i<=1000;$i++)
{
    $var = array();
    $var['booleon'] = true;
}
$total_array = microtime(true) - $ts_a;

echo 'Object: ' . ($total_object)  . ' / Array: ' . $total_array;

New Results: 0.0037809 / Array: 0.0046189

There's a few test you would have to do then find your mean / mode at the end of the test to find the one that truly is the better entity.

You can do a test on memory by doing a memory_get_usage : http://php.net/manual/en/function.memory-get-usage.php with the same principles.

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • 18
    IMO performance considerations shoudnt be going into a decision whether to use the one or the other. – Gordon Sep 14 '10 at 14:21
  • 1
    Test writing to them. Test passing them to functions. Test passing by reference and modifying them inside of the function (hint, with objects you don't need to pass by reference to modify inside the function). Creation is probably the least often things done with either arrays or objects. Who cares if they are cheap to create if they are expensive to use... And besides, you're talking about the difference of `1x10^-6`...If you're worried about that little gain, you're wasting your time. Remember, `Premature Optimization Is The Root Of All Evil`... – ircmaxell Sep 14 '10 at 14:33
  • 1
    But performance in general is a major part of any successful application, and should be address whenever a decisions / implementation is instructed! – RobertPitt Sep 14 '10 at 14:34
  • 1
    +1, Premature Anything is the root of all evil. – RobertPitt Sep 14 '10 at 14:36
  • 2
    Well, let's rephrase it "to performance considerations shouldnt be your *primary concern* when deciding which to use". And if they are a concern, let's not forget the [new Spl Data Structures and how they perform](http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/). – Gordon Sep 14 '10 at 14:52
7

What are you doing? Neither structure is better at everything, which is why both exist. So it really depends on your problem set which is "better". And even then, it may simply be a case of which you prefer. A lot of architecture and code design is personal preference and style.

Now, in general, there is only one time I would use a method-less object (stdclass, or a custom defined one) over an array. That's if I need to pass that data around a lot and modify it in multiple places. With an array, you'll need to pass it by reference, which gets cumbersome and can introduce sources of error... With an object, it's passed as an object reference by default (not a variable reference, but a pointer to the same object). So with an array you'd need to do something like:

function doSomething($inVar, array &$inOutArray) {
    $inOutArray['bar'] = 'baz';
}

For each function that modifies (or might modify) the array.

Whereas with an object, you could just do:

function doSomething($inVar, $object) {
    $object->bar = 'baz';
}

It's shorter (sure, only one character, but it's one character everywhere you might want to modify the array). It's less prone to bugs, since if you later do $object = new Something(), it won't change the original object (since it's not a variable reference)...

The only argument is that it's slightly less readable, since the & shows you explicitly that you intend to modify that input variable. But if you understand how objects work in PHP 5, you shouldn't be caught off guard (and hence it's an acceptable tradeoff in my mind)...

ircmaxell
  • 163,128
  • 34
  • 264
  • 314