单一实例化的实现

原创 2018-12-16 10:48:27 219
摘要:<?phpclass Hubby{    private function __construct()    {    }    private function __clone()    {        // TODO: Implement __cl

<?php
class Hubby
{
   private function __construct()
   {
   }

   private function __clone()
   {
       // TODO: Implement __clone() method.
   }

   protected static $instance = null;

   public static function getInstance()
   {
       if (is_null(static::$instance)) {
           static::$instance = new static();

       }
       return static::$instance;
   }
}
$hubby1 = Hubby::getInstance();
$hubby2 = Hubby::getInstance();
echo ($hubby1 instanceof Hubby)?'是':'不是';
echo '<hr>';
echo ($hubby2 instanceof Hubby)?'是':'不是';
echo '<hr>';
echo ($hubby1 ===$hubby2)?'完全相等':'不相等';
echo  '<hr>';
var_dump($hubby1,$hubby2);

批改老师:天蓬老师批改时间:2018-12-16 10:49:18
老师总结:相信你对单例模式有了了解,在js中,其实都是单例的

发布手记

热门词条