Created
November 16, 2020 12:01
-
-
Save kobus1998/357d6331b043af315a1a486260f1327e to your computer and use it in GitHub Desktop.
Singleton extend class
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 | |
class singleton | |
{ | |
/** | |
* protect the construct | |
*/ | |
protected function __construct() | |
{ | |
} | |
/** | |
* get instance | |
* | |
* @return static | |
*/ | |
public static function instance() | |
{ | |
static $instances = array(); // all instances for different classes are saved in this array | |
$calledClass = get_called_class(); // gets the name of the class | |
if (!isset($instances[$calledClass])) { // create a new one if not set | |
$instances[$calledClass] = new $calledClass(); | |
} | |
return $instances[$calledClass]; | |
} | |
/** | |
* protected the call function | |
* | |
* @param string $function | |
* @param array $args | |
* @return void | |
*/ | |
protected function __call($function, $args) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment