Federico J.
Federico J.

Reputation: 15882

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

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?

Upvotes: 15

Views: 11351

Answers (3)

Alex
Alex

Reputation: 1593

Based on small test 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.

Upvotes: 10

Touki
Touki

Reputation: 7525

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.

Upvotes: 4

davidkonrad
davidkonrad

Reputation: 85518

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.

Upvotes: 9

Related Questions