"new stdClass" VS "(object) NULL"

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I came across an odd piece of PHP code today that I've never seen before and I'm trying to understand the reasoning behind it.

PHP Code: [ Select ]
  $this->chart = (object) NULL;
  // $chart = new stdClass();
  1.   $this->chart = (object) NULL;
  2.   // $chart = new stdClass();


It seems apparent that the stdClass was commented out in favor of type casting null to an object. Why would someone do this ?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I think its just a matter of style. I dont believe there is any difference whats so ever.

PHP Code: [ Select ]
$obj = (object) null;
print is_a($obj, 'stdClass') ? 1 : 0;
  1. $obj = (object) null;
  2. print is_a($obj, 'stdClass') ? 1 : 0;


That prints 1.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I just did a test where I created 100 array elements using each of the two methods.

On the first run, before my opcode cache gets a chance to cache it, it used 196 bytes less memory to use stdClass. On the second run, after the opcode cache kicked in, the stdClass method used 320 bytes less memory.

It makes no difference in the memory used whether or not you use "stdClass();" or "stdClass;".

PHP Code: [ Select ]
<?php
 
$mem =  memory_get_usage(false);
echo $mem . '<br/>';
 
$one = array();
$two = array();
$three = array();
$i = 0;
$j = 0;
$k = 0;
 
$mem2 =  memory_get_usage(false);
echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
$mem = $mem2;
 
for($j = 0; $j < 100; $j++)
{
   $two[] = (object) NULL;
}
 
$mem2 =  memory_get_usage(false);
echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
$mem = $mem2;
 
for($i = 0; $i < 100; $i++)
{
   $one[] = new stdClass();
}
 
$mem2 =  memory_get_usage(false);
echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
$mem = $mem2;
 
for($k = 0; $k < 100; $k++)
{
   $three[] = new stdClass;
}
 
$mem2 =  memory_get_usage(false);
echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
$mem = $mem2;
 
?>
  1. <?php
  2.  
  3. $mem =  memory_get_usage(false);
  4. echo $mem . '<br/>';
  5.  
  6. $one = array();
  7. $two = array();
  8. $three = array();
  9. $i = 0;
  10. $j = 0;
  11. $k = 0;
  12.  
  13. $mem2 =  memory_get_usage(false);
  14. echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
  15. $mem = $mem2;
  16.  
  17. for($j = 0; $j < 100; $j++)
  18. {
  19.    $two[] = (object) NULL;
  20. }
  21.  
  22. $mem2 =  memory_get_usage(false);
  23. echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
  24. $mem = $mem2;
  25.  
  26. for($i = 0; $i < 100; $i++)
  27. {
  28.    $one[] = new stdClass();
  29. }
  30.  
  31. $mem2 =  memory_get_usage(false);
  32. echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
  33. $mem = $mem2;
  34.  
  35. for($k = 0; $k < 100; $k++)
  36. {
  37.    $three[] = new stdClass;
  38. }
  39.  
  40. $mem2 =  memory_get_usage(false);
  41. echo $mem2 . ' (+' . ($mem2 - $mem) . ')<br/>';
  42. $mem = $mem2;
  43.  
  44. ?>


I also took a look at the time taken to create 100K instances. The difference is negligible at about 0.015 seconds difference for the 100K instances. It does seem that "(object) NULL" is always slower than "new stdClass()" and both slower than "new stdClass;". Both with and without the opcode cache kicking in.

Omiting the "new" operator from before "stdClass;" nearly doubles the time taken. I'm not sure it would even work properly without that operator. :scratchhead:

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.