Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created November 16, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/357d6331b043af315a1a486260f1327e to your computer and use it in GitHub Desktop.
Save kobus1998/357d6331b043af315a1a486260f1327e to your computer and use it in GitHub Desktop.
Singleton extend class
<?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