PHP單例模式實例分析【防繼承,防克隆操作】
本文實例講述了PHP單例模式。分享給大家供大家參考,具體如下:
<?php //單列模式 // //1.普通類 // class singleton{ // } // $s1 = new singleton(); // $s2 = new singleton(); // //注意,2個變量是同1個對象的時候才全等 // if ($s1 === $s2) { // echo '是一個對象'; // }else{ // echo '不是一個對象'; // } // //2.封鎖new操作 // class singleton{ // protected function __construct(){} // } // $s1 = new singleton();//PHP Fatal error: Call to protected singleton::__construct() // //3.留個接口來new對象 // class singleton{ // protected function __construct(){} // public static function getIns(){ // return new self(); // } // } // $s1 = singleton::getIns(); // $s2 = singleton::getIns(); // if ($s1 === $s2) { // echo '是一個對象'; // }else{ // echo '不是一個對象'; // } // //4.getIns先判斷實例 // 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 '是一個對象'; // }else{ // echo '不是一個對象'; // } // //繼承 // class A extends singleton{ // public function __construct(){} // } // echo '<br>'; // $s1 = new A(); // $s2 = new A(); // if ($s1 === $s2) { // echo '是同一個對象'; // }else{ // echo '不是同一個對象'; // } // //5.防止繼承時被修改了權(quán)限 // class singleton{ // protected static $ins = null; // //方法加final則方法不能被覆蓋,類加final則類不能被繼承 // 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 '是同一個對象'; // }else{ // echo '不是同一個對象'; // } // //繼承 // // 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 '是同一個對象'; // }else{ // echo '不是同一個對象'; // } //6.防止被clone class singleton{ protected static $ins = null; //方法加final則方法不能被覆蓋,類加final則類不能被繼承 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 '是同一個對象'; }else{ echo '不是同一個對象'; }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
用PHP寫的基于Memcache的Queue實現(xiàn)代碼
用PHP寫的基于Memcache的Queue實現(xiàn)代碼,需要的朋友可以參考下。2011-11-11PHP面向?qū)ο蟪绦蛟O(shè)計之接口的繼承定義與用法詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計之接口的繼承定義與用法,結(jié)合實例形式分析了php接口的概念、定義、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2018-12-12PHP fastcgi模式上傳大文件(大約有300多K)報錯
上傳圖片時,大約有300多K,結(jié)果報了個服務(wù)器錯誤,fastcgi默認(rèn)的請求大小為131072,于是在apache配置中添加了MaxRequestLen 配置就好了2014-09-09