单例模式(防继承,防克隆)

本文详细介绍了单例模式的实现步骤,包括如何通过限制构造函数访问、提供静态获取实例的方法、确保实例唯一性以及防止克隆等方式来实现。此外,还讨论了如何避免通过继承破坏单例模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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 '不是同一个对象';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值