单例模式:
模式:提供创建的静态方法或在构造函数中创建。常用是静态方法。
1.构造函数私有访问属性
2.静态的指向本类的指针
3.静态的创建自身的方法
以下是php中的实现,getinstall理论上应该加上线程同步,暂未写。
<?phpclassmyclass {publicstatic$mydata1=NULL;
privatefunction__construct(){echo"构造函数<br>";
}
publicstaticfunctiongetinstall() {if ( self::$mydata1==null)
{
echo"创建对象<br>";
self::$mydata1 = new myclass();
}
returnself::$mydata1;
}
publicfunctiontest()
{echo"hello2<br>";
}
}
$f = myclass::getinstall();
$f->test();
$f2 = myclass::getinstall();
$f2->test();
?>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了 php入门2 单例模式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










