10

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?

MM PP
  • 4,050
  • 9
  • 35
  • 61
  • Closing as opinion-based, but personally I prefer arrays because there's a bunch of useful array functions that you just can't use on stdClasses. – Niet the Dark Absol Jul 02 '14 at 09:35
  • Downvoting. Useless question, you can't ask which is *best*. You should think "what approach am I supposed to use to make life easier for myself and my colleagues". Speed wise - it's irrelevant. – N.B. Jul 02 '14 at 09:36
  • possible duplicate of [What is better stdClass or (object) array to store related data?](http://stackoverflow.com/questions/18640607/what-is-better-stdclass-or-object-array-to-store-related-data) – mvp Jul 02 '14 at 09:38
  • 1
    @KA_lin what on earth are you talking about? Using an array or an object is not procedural v OOP. Please dont confuse people about things like that. – Phil Sturgeon Jul 02 '14 at 09:45
  • 1
    @PhilSturgeon: I didn`t formulated correctly sorry b`out that :) – ka_lin Jul 02 '14 at 09:51
  • 4
    Strongly disagree about closing the question. Maybe it's a bit too broad, but I'm reading it as 'what are pros&cons of using assoc array over stdClass?' And I'm sorry for people who close the question because they don't feel like or are not able to provide a meaningful answer. – tishma Nov 01 '16 at 10:07
  • 1
    There's a foggy area where opinions cross over to a debate about advantages and disadvantages. Both of them are critical for understanding the contents as it brings forward various important contributing factors. I strongly disagree with Stackoverflow's policy of closing opinionated questions. – Glitch Oct 28 '17 at 01:27

1 Answers1

14

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:

  1. For arrays, PHP 5.5 is faster than PHP 5.4, for object it is pretty much the same
  2. Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays.
  3. stdClass is evil.
  4. Class still uses less memory than Arrays. (about 30-40% less!!).
  5. SplFixedArray is similar to use a Class but it uses more memory.
Community
  • 1
  • 1
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • 1
    You can pass true as a 2nd argument to json_decode to get the results back as arrays rather than stdClass. In my scenario I was writing out JSON to a file, then parsing it back on next execution, but array functions were not working on stdClass go figure... – Liam Mitchell Nov 12 '20 at 22:00