Skip to content

Instantly share code, notes, and snippets.

@vasilake-v
Created November 8, 2018 14:45
Show Gist options
  • Save vasilake-v/7b006f77ecfb728971f9c83f9c8fe61f to your computer and use it in GitHub Desktop.
Save vasilake-v/7b006f77ecfb728971f9c83f9c8fe61f to your computer and use it in GitHub Desktop.
Singletone magic
<?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');
@vasilake-v
Copy link
Author

output:
-----
testA12
testB3
testB34
testA125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment