作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例。它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用。
单例模式特点:
- 只能有一个实例。
- 必须自行创建这个实例。
- 必须给其他对象提供这一实例。
namespace app\common\lib\rredis;
class Predis{
public $redis = "";
private status $_instance = null;
public static function getInstance(){
if(empty(self::$instance)){
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct(){
$this->redis = new \Redis():
$this->redis->connect(config('redis.hostt'),config('redis.port'),config('redis.timeOut');
}