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';
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';
Arrays
array_*
functions that can work on arrays, most of which are very fast.Objects
__get
, __set
, etc)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.
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)...