Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better stdClass or (object) array to store related data?

Tags:

object

oop

php

I have been using arrays to store related fields during a long time. If I wanted to have related user fields, I used:

$user = array(
   'id' => 27
   'name' => 'Pepe'
);

But lately, I've been working a lot with objects, and I like it more to use $user->id instead of $user['id'].

My question: To achieve an object oriented style, you may use stdClass:

$user = new stdClass();
$user->id = 27;
$user->name = 'Pepe';

or casting from an array

$user = (object) array(
  'id' => 27
, 'name' => 'Pepe'
);

Is one of them better than the other, in order of performance and style, or can you use whatever you want indistinctly?

Thanks!

Update: I agree with all the comments, this is not OOP at all, is just about having related data grouped into a structure. My $user example is not the best, because it's a typical example of using classes with method, properties, blablabla... I asked because I have a lot of config structures, such us "initTable", and I want something like:

$table => page => init => 1
               => end  => 25
          sort => field => name
               => order => asc

and so on, and I want to know what is better to get init page:

$table->page->init **OR** $table['page']['init']
like image 538
Federico J. Avatar asked Sep 05 '13 15:09

Federico J.


People also ask

Which is faster array or object in PHP?

The difference between an array numeric and an associative array is a mere 5%, so you can say that they are the same. The use of an object is +100% slower but it is still acceptable in most conditions (aka it uses the double of time).

Is stdClass an object?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

What is the difference between array and object in PHP?

An object is an instance of a class. It is simply a specimen of a class and has memory allocated. Array is the data structure that stores one or more similar type of values in a single name but associative array is different from a simple PHP array.

Are arrays objects in PHP?

Yes, its possible to have array of objects in PHP.


2 Answers

Based on small test (http://phpfiddle.org/lite/code/cz0-hyf) I can say that using "new stdClass()" is about 3 times slower 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.

like image 116
Alex Avatar answered Oct 07 '22 18:10

Alex


Try investigate the issue, here a test doing each 1.000.000 times each :

$start = microtime(true);
for ($i=0;$i<1000000;$i++) {
    $user = new stdClass();
    $user->id = 27;
    $user->name = 'Pepe';
}
$end = microtime(true);
echo $end - $start;

echo '<br><br>';

$start = microtime(true);
for ($i=0;$i<1000000;$i++) {
    $user = (object) array(
        'id' => 27, 
        'name' => 'Pepe'
    );
}
$end = microtime(true);
echo $end - $start;

reports

0.75109791755676
0.51117610931396

so - appearently, in this particular case - casting from array is the fastest way to do it. Beats stdClass with many percent. But I wouldnt count on it as a general universal rule or law.

like image 28
davidkonrad Avatar answered Oct 07 '22 18:10

davidkonrad