PHP单例模式实例分析【防继承,防克隆操作】

yipeiwu_com5年前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.防止继承时被修改了权限
// 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 '不是同一个对象';
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP的拦截器实例分析

本文实例讲述了PHP的拦截器用法。分享给大家供大家参考。具体如下: PHP提供了几个拦截器,用于在访问未定义的方法和属性时被调用,如下所示: 1、__get($property) 功能:...

php通过COM类调用组件的实现代码

在PHP 4.2.0 至 4.2.3中,可以使用w32api_register_function 函数调用外部的DLL,前提是需要在php.ini中打开扩展的php_w32api.dll...

PHP GD库添加freetype拓展的方法

背景:业务需求要用到 imagefttext 函数,发现GD库一开始安装时没有添加 FreeType linux版本 centos 6.6 安装流程(由于服务器为分布式内网服务器,无法使...

php判断GIF图片是否为动画的方法

本文介绍了PHP判断GIF图片是动画的方法,具体步骤如下: 首先,gif动画是gif89格式的,发现文件开头是gif89。但是很多透明图片也是用的gif89格式, GOOGLE到的:可以...

解析PHP留言本模块主要功能的函数说明(代码可实现)

一,敏感词处理1,过滤敏感词preg_match()函数用来在字符串中搜索所有与给定的正则表达式匹配的内容,如果存在则返回True,否则返回False。语法:int preg_match...