PHP 的單例模式代碼
class User {
static function getInstance()
{
if (self::$instance == NULL) { // If instance is not created yet, will create it.
self::$instance = new User();
}
return self::$instance;
}
private function construct()
// Constructor method as private so by mistake developer not crate
// second object of the User class with the use of new operator
{
}
private function clone()
// Clone method as private so by mistake developer not crate
//second object of the User class with the use of clone.
{
}
function Log($str)
{
echo $str;
}
static private $instance = NULL;
}
User::getInstance()->Log("Welcome User");</pre>
本文由用戶 eb5y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!