<?php
trait Singletons{
private static $instance;
public static function getInstance(){
if(!isset(self::$instance)){
self::$instance = new static();//这里不能new self(),self和static区别
}
return self::$instance;
}
}
class BaseSingle{
//trait用法
use Singletons;
private $redis;
public function __construct()
{
//封装redis实例作为测试
$this->redis = new \Redis();
$this->redis->pconnect("127.0.0.1",6379);
}
/**
* 内部访问方法
*/
public function get(){
echo $this->redis;
echo "111";
}
}
//调用测试
BaseSingle::getInstance()->get();