本文實(shí)例講述了PHP單例模式。分享給大家供大家參考,具體如下:
?php
//單列模式
// //1.普通類(lèi)
// class singleton{
// }
// $s1 = new singleton();
// $s2 = new singleton();
// //注意,2個(gè)變量是同1個(gè)對(duì)象的時(shí)候才全等
// if ($s1 === $s2) {
// echo '是一個(gè)對(duì)象';
// }else{
// echo '不是一個(gè)對(duì)象';
// }
// //2.封鎖new操作
// class singleton{
// protected function __construct(){}
// }
// $s1 = new singleton();//PHP Fatal error: Call to protected singleton::__construct()
// //3.留個(gè)接口來(lái)new對(duì)象
// class singleton{
// protected function __construct(){}
// public static function getIns(){
// return new self();
// }
// }
// $s1 = singleton::getIns();
// $s2 = singleton::getIns();
// if ($s1 === $s2) {
// echo '是一個(gè)對(duì)象';
// }else{
// echo '不是一個(gè)對(duì)象';
// }
// //4.getIns先判斷實(shí)例
// class singleton{
// protected static $ins = null;
// private function __construct(){}
// public static function getIns(){
// if (self::$ins === null) {
// self::$ins = new self();
// }
// return self::$ins;
// }
// }
// $s1 = singleton::getIns();
// $s2 = singleton::getIns();
// if ($s1 === $s2) {
// echo '是一個(gè)對(duì)象';
// }else{
// echo '不是一個(gè)對(duì)象';
// }
// //繼承
// class A extends singleton{
// public function __construct(){}
// }
// echo 'br>';
// $s1 = new A();
// $s2 = new A();
// if ($s1 === $s2) {
// echo '是同一個(gè)對(duì)象';
// }else{
// echo '不是同一個(gè)對(duì)象';
// }
// //5.防止繼承時(shí)被修改了權(quán)限
// class singleton{
// protected static $ins = null;
// //方法加final則方法不能被覆蓋,類(lèi)加final則類(lèi)不能被繼承
// final private function __construct(){}
// public static function getIns(){
// if (self::$ins === null) {
// self::$ins = new self();
// }
// return self::$ins;
// }
// }
// $s1 = singleton::getIns();
// $s2 = singleton::getIns();
// if ($s1 === $s2) {
// echo '是同一個(gè)對(duì)象';
// }else{
// echo '不是同一個(gè)對(duì)象';
// }
// //繼承
// // class A extends singleton{
// // public function __construct(){}
// // }
// //Cannot override final method singleton::__construct()
// echo 'hr>';
// $s1 = singleton::getIns();
// $s2 = clone $s1;
// if ($s1 === $s2) {
// echo '是同一個(gè)對(duì)象';
// }else{
// echo '不是同一個(gè)對(duì)象';
// }
//6.防止被clone
class singleton{
protected static $ins = null;
//方法加final則方法不能被覆蓋,類(lèi)加final則類(lèi)不能被繼承
final private function __construct(){}
public static function getIns(){
if (self::$ins === null) {
self::$ins = new self();
}
return self::$ins;
}
// 封鎖clone
final private function __clone(){}
}
$s1 = singleton::getIns();
$s2 = clone $s1; //Call to private singleton::__clone() from context
if ($s1 === $s2) {
echo '是同一個(gè)對(duì)象';
}else{
echo '不是同一個(gè)對(duì)象';
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP 工廠(chǎng)模式使用方法
- php設(shè)計(jì)模式 Factory(工廠(chǎng)模式)
- PHP中“簡(jiǎn)單工廠(chǎng)模式”實(shí)例代碼講解
- php基礎(chǔ)設(shè)計(jì)模式大全(注冊(cè)樹(shù)模式、工廠(chǎng)模式、單列模式)
- PHP實(shí)現(xiàn)單例模式最安全的做法
- php單例模式實(shí)現(xiàn)(對(duì)象只被創(chuàng)建一次)
- php利用單例模式實(shí)現(xiàn)日志處理類(lèi)庫(kù)
- PHP設(shè)計(jì)模式之注冊(cè)樹(shù)模式分析
- PHP單例模式數(shù)據(jù)庫(kù)連接類(lèi)與頁(yè)面靜態(tài)化實(shí)現(xiàn)方法
- PHP工廠(chǎng)模式、單例模式與注冊(cè)樹(shù)模式實(shí)例詳解