What is better stdClass or (object) array to store

2020-05-25 18:01发布

问题:

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']

回答1:

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.



回答2:

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.



回答3:

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.



标签: php oop object