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']
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).
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.
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.
Yes, its possible to have array of objects in PHP.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With