Home >Backend Development >PHP Tutorial >php design pattern singleton
To allow a class to have only one instance, no cloning
class Single{
//Static methods can only reference static variables
private static $_instance;
//防止外部使用new创建对象,单例类不能在其它类实例化,只能被自身类实例化
//需要获取静态方法,返回唯一实例的引用
$dan1=Single::$_instance; //dan2和dan1对象完全一样,但由于dan1的静态方法已经创建自身实例, //所以dan2只是返回实例引用 $dan2=Single::$_instance;
if($danli===$dandi){ echo 'total'; //完全相等 }
1. Database application: Use singletons to avoid a lot of new wasted resources
2. The system needs global classes to control
3. Page requests for easy debugging
The above introduces the PHP design pattern singleton, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.