Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyClass {
- // The singleton
- private static $msSingleton = null;
- // A number to check the singleton.
- public $foo = 0;
- private function __construct()
- {
- echo "Construct the class\n";
- }
- public static function getSingleton()
- {
- if(is_null(self::$msSingleton)) {
- self::$msSingleton = new MyClass();
- }
- return self::$msSingleton;
- }
- public function __call($name, $arguments)
- {
- echo '__call called. $name = ' . $name . "\n";
- // Like the overriding of the new operator of C++.
- if($name == '__construct') {
- return self::getSingleton();
- }
- }
- }
- $a = MyClass::getSingleton();
- $a->foo = 20;
- $b = MyClass::getSingleton();
- echo $b->foo . "\n";
- $b->foo = 27;
- echo $a->foo . "\n";
- $c = $a->__construct();
- $c->foo = 32;
- echo $a->foo . "\n";
- $d = new MyClass();
- echo $d->foo . "\n";
- $d->foo = 45;
- echo $a->foo . "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement