Created
November 8, 2018 14:45
-
-
Save vasilake-v/7b006f77ecfb728971f9c83f9c8fe61f to your computer and use it in GitHub Desktop.
Singletone magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait singletone | |
{ | |
static function getInstance() { | |
static $instance; | |
if ($instance === null){ | |
$instance = new self(); | |
} | |
return $instance; | |
} | |
} | |
class a { | |
use singletone; | |
private $name; | |
public function test($i){ | |
self::getInstance()->name .= $i; | |
echo "\ntestA" . self::getInstance()->name; | |
} | |
} | |
class b { | |
use singletone; | |
private $name; | |
public function test($i){ | |
self::getInstance()->name .= $i; | |
echo "\ntestB" . self::getInstance()->name; | |
} | |
} | |
a::getInstance()->test('1'); | |
a::getInstance()->test('2'); | |
b::getInstance()->test('3'); | |
b::getInstance()->test('4'); | |
a::getInstance()->test('5'); |
Author
vasilake-v
commented
Nov 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment