/*
php通用单例模式
*/
class Singleton
{
private static $_singleton;
private function __construct()
{
echo 'no new\n';
}
public static function getInstance($air,$obj)
{
self::$_singleton[$air] = !isset(self::$_singleton[$air])?$obj:self::$_singleton[$air];
return self::$_singleton[$air];
}
private function __clone()
{
trigger_error('no clone\n',E_USER_ERROR);
}
}
/**
* 案例
*/
class man
{
private function __construct()
{
//echo 'no new\n';
}
public function test()
{
echo 'input man test';
}
public static function getInstance()
{
$singleton = Singleton::getInstance('man',new self());
return $singleton;
}
private function __clone()
{
trigger_error('no clone\n',E_USER_ERROR);
}
}
领取专属 10元无门槛券
私享最新 技术干货