设计模式案例(个人笔记)

一、单例模式,示例:

<?php

class Predis{

  public $redis = "";

  private static $_instance = null;

  public static function getInstance() {

    if(empty(self::$_instance)) {

      self::$_instance = new self();

    }

    return self::$_instance;

  }

  private function __construct() {

    $this->redis = new \Redis();

    $result = $this->redis->connect(config('redis.host'), config('redis.port'), config('redis.timeOut'));

    if($result === false) {

      throw new \Exception('redis connect error');

    }

  }

}

猜你喜欢

转载自www.cnblogs.com/zhengchuzhou/p/9617279.html