单例模式一个小练习

原创 2018-10-28 20:28:29 126
摘要:<?php//最终类不能被其他类继承final class Cat{      //私有静态保存实例的属性   private static $instance;   //阻止外部new对象   private function __construct(){}   &nbs

<?php


//最终类不能被其他类继承

final class Cat{

   

   //私有静态保存实例的属性

   private static $instance;

   //阻止外部new对象

   private function __construct(){}

   

   //阻止克隆

   private function __clone(){}

   //获取对象实例 存在则返回

   public static function getSingleton(){

   if(! self::$instance instanceof self){

self::$instance = new self();

}

return self::$instance;

   } 

}


$cat = Cat::getSingleton();

var_dump($cat);


发布手记

热门词条