Advertisement
pierovdfn

PHP __construct bug

Jun 18th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. <?php
  2.  
  3. class MyClass {
  4.  
  5.     // The singleton
  6.     private static $msSingleton = null;
  7.  
  8.     // A number to check the singleton.
  9.     public $foo = 0;
  10.  
  11.     private function __construct()
  12.     {
  13.         echo "Construct the class\n";
  14.     }
  15.  
  16.     public static function getSingleton()
  17.     {
  18.         if(is_null(self::$msSingleton)) {
  19.             self::$msSingleton = new MyClass();
  20.         }
  21.  
  22.         return self::$msSingleton;
  23.     }
  24.  
  25.     public function __call($name, $arguments)
  26.     {
  27.         echo '__call called. $name = ' . $name . "\n";
  28.         // Like the overriding of the new operator of C++.
  29.         if($name == '__construct') {
  30.             return self::getSingleton();
  31.         }
  32.     }
  33.  
  34. }
  35.  
  36. $a = MyClass::getSingleton();
  37. $a->foo = 20;
  38.  
  39. $b = MyClass::getSingleton();
  40. echo $b->foo . "\n";
  41. $b->foo = 27;
  42. echo $a->foo . "\n";
  43.  
  44. $c = $a->__construct();
  45. $c->foo = 32;
  46. echo $a->foo . "\n";
  47.  
  48. $d = new MyClass();
  49. echo $d->foo . "\n";
  50. $d->foo = 45;
  51. echo $a->foo . "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement