title: static var and functions: memory allocation in php
body: <p>I have a doubt in memory allocation with a php 5.3 script.
  Imagine you have 2 static classes (MyData and Test) like these:</p>
  
  <pre><code>class MyData {
      private static $data = null;
  
      public static function getData() {
          if(self::$data == null)
              self::$data = array(1,2,3,4,5,);
          return self::$data;
      }
  }
  
  class Test {
      private static $test_data = null;
  
      public static function getTestData1() {
          if(self::$test_data==null) {
              self::$test_data = MyData::getData();
              self::$test_data[] = 6;
          }
          return self::$test_data;
      }
  
      public static function getTestData2() {
          $test = MyData::getData();
          $test[] = 6;
          return $test;
      }
  }
  </code></pre>
  
  <p>And a simple test.php script:</p>
  
  <pre><code>for($i = 0; $i &lt; 200000; $i++) {
      echo "Pre-data1 Test:\n\t" . memory_get_usage(true) . "\n";
      Test::getTestData1();
      echo "Post-data1 Test:\n\t" . memory_get_usage(true) . "\n";
  }
  
  for($i = 0; $i &lt; 200000; $i++) {
      echo "Pre-data2 Test:\n\t" . memory_get_usage(true) . "\n";
      Test::getTestData2();
      echo "Post-data2 Test:\n\t" . memory_get_usage(true) . "\n";
  }
  </code></pre>
  
  <p>I might suppose that the call to Test::getTestData1() will alloc memory for 2 static variables, while Test::getTestData2() will destroy $test (the copy of the static variable) on function return, so the second call is less "memory expensive".</p>
  
  <p>But if I run the test.php script, memory_get_usage will show the same values for Test::getTestData1() and Test::getTestData2()</p>
  
  <p>Why?</p>