码迷,mamicode.com
首页 > Web开发 > 详细

PHP设计模式之单例模式

时间:2017-04-22 13:10:44      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:对象   func   public   ted   ret   null   protected   tor   stat   

    <?php
    #千锋PHP http://www.qfedu.com/php/?
    #千锋PHP http://www.qfedu.com/php/?
    namespace app;
    
    /**
     * Class Person
     *
     * @package app
     */
    class Person
    {
  /**
   * 保存对象实例
   * @var
   */
  protected static $isMake;
    
  /**
   * 不允许外部实例化
   * Person constructor.
   */
  private function __construct()
  {
    
  }
    
  /**
   * 使用静态变量控制只会创建一次对象实例
   *
   * @return mixed
   */
  public static function make()
  {
      if (is_null(static::$isMake)) {
          static::$isMake = new static();
      }
    
      return static::$isMake;
  }
    }
    
    $instance = Person::make();
    

 

PHP设计模式之单例模式

标签:对象   func   public   ted   ret   null   protected   tor   stat   

原文地址:http://www.cnblogs.com/gaohj/p/6746891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!