最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP單例模式實(shí)例分析【防繼承,防克隆操作】

 更新時(shí)間:2019年05月22日 11:43:53   作者:學(xué)習(xí)筆記666  
這篇文章主要介紹了PHP單例模式,結(jié)合實(shí)例形式分析了php單例模式的定義,以及php防繼承,防克隆等操作技巧,代碼注釋中備有詳盡的說明,需要的朋友可以參考下

本文實(shí)例講述了PHP單例模式。分享給大家供大家參考,具體如下:

<?php
//單列模式
// //1.普通類
// 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è)接口來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則方法不能被覆蓋,類加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 '是同一個(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則方法不能被覆蓋,類加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 '是同一個(gè)對(duì)象';
}else{
  echo '不是同一個(gè)對(duì)象';
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

锦屏县| 友谊县| 筠连县| 武鸣县| 连州市| 钟祥市| 来安县| 清河县| 镇宁| 阿克苏市| 乐亭县| 灵武市| 青阳县| 桐梓县| 浙江省| 黄龙县| 勃利县| 隆回县| 大化| 孝感市| 武穴市| 蓬莱市| 修文县| 象山县| 林口县| 三穗县| 民县| 扶绥县| 华亭县| 定州市| 普宁市| 原平市| 东乡| 德令哈市| 合阳县| 昭通市| 西华县| 商河县| 天气| 新泰市| 沙田区|