18

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']
Federico J.
  • 15,388
  • 6
  • 32
  • 51
  • Instinctively I would say creating an object is better than creating an array and casting it to an object in terms of memory use and general performance. – mhafellner Sep 05 '13 at 15:43
  • 6
    just because something is in an object, doesn't mean your using OOP. – Prisoner Sep 05 '13 at 15:44
  • If you use an actual typed object, your IDE can perform intellisense/correctness checking. Subjectively that is much more useful than microscopic differences between these choices. – DCoder Sep 05 '13 at 15:45
  • 2
    If you really want to do OOP use classes. – Jimmy T. Sep 05 '13 at 15:48
  • @JimmyT.: Agreed. `stdClass` and `(object) array` end up giving you the same thing...which is basically an array with funky syntax, reference semantics, and the lack of most of what makes either arrays *or* objects worthwhile. OOP involves attaching behavior to the data, which you can't do like this. – cHao Sep 05 '13 at 15:58

3 Answers3

11

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.

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Alex
  • 1,605
  • 11
  • 14
8

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.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
4

I don't think moving an array to a standard object would make a difference. Except that you can't use most of the array_* functions anymore.
I can't see any advantages using a dynamic object over an indexed array.

In this case, I would create a class for each element and properties you'd need.

class User
{
    protected $id;
    protected $name;

    public function __construct($id = null, $name = null)
    {
        $this->id   = $id;
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

With such a design, you know exactly what kind of parameters you have.

$user  = new User(21, 'Foo');
echo $user->getName(); // Foo

$blank = new User(22);
echo $blank->getName(); // NULL

No errors, no more checking.
With an array or stdClass you would have something like that

$user = array('id' => 21, 'name' => 'Foo');
echo $user['name']; // Foo

$blank = array('id' => 22);
echo $blank['name']; // NOTICE: Undefined offset
echo isset($blank['name']) ? $blank['name'] : null; // NULL

For this kind of behaviour, having a solid object in which you know the interface is easier to maintain and to twist.

Touki
  • 7,465
  • 3
  • 41
  • 63