all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
21
Task/Singleton/PHP/singleton.php
Normal file
21
Task/Singleton/PHP/singleton.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Singleton {
|
||||
protected static $instance = null;
|
||||
public $test_var;
|
||||
private function __construct(){
|
||||
//Any constructor code
|
||||
}
|
||||
public static function getInstance(){
|
||||
if (is_null(self::$instance)){
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
|
||||
$foo = Singleton::getInstance();
|
||||
$foo->test_var = 'One';
|
||||
|
||||
$bar = Singleton::getInstance();
|
||||
echo $bar->test_var; //Prints 'One'
|
||||
|
||||
$fail = new Singleton(); //Fatal error
|
||||
Loading…
Add table
Add a link
Reference in a new issue