Where is recommended to use one of them? I want to store data from articles listed from db.
It's a simple question:
echo $Datastore->name; //I like it works with foreach
//vs
echo $Datastore['name'];
Which is the best? Is there any difference between stdClass
and array
speed of getting elements?
There is a similar question What is better stdClass or (object) array to store related data? with this answer
Based on small test (http://phpfiddle.org/lite/code/cz0-hyf) I can say that using "new stdClass()" is about 3 times slowlier than other options.
It is strange, but casting an array is done very efficiently compared to stdClass.
But this test meters only execution time. It does not meter memory.
P.S. I used phpFiddle only to share code. Test were done at my local PC.
In answer to another similar question you can see this conclusion:
- For arrays, PHP 5.5 is faster than PHP 5.4, for object it is pretty much the same
- Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays.
- stdClass is evil.
- Class still uses less memory than Arrays. (about 30-40% less!!).
- SplFixedArray is similar to use a Class but it uses more memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With