时间:2021-07-01 10:21:17 帮助过:20人阅读
<?php
/**
* Created by PhpStorm.
* User: yangyulong
* Date: 2015/5/26
* Time: 13:45
*/
class Mongo_db
{
private static $instanceof = NULL;
public $mongo;
private $host = 'localhost';
private $port = '27017';
private $db;
public $dbname;
private $table = NULL;
/**
* 初始化类,得到mongo的实例对象
*/
public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL)
{
if (NULL === $dbname) {
$this->throwError('集合不能为空!');
}
//判断是否传递了host和port
if (NULL !== $host) {
$this->host = $host;
}
if (NULL !== $port) {
$this->port = $port;
}
$this->table = $table;
$this->mongo = new MongoClient($this->host . ':' . $this->port);
if ($this->getVersion() >= '0.9.0') {
$this->dbname = $this->mongo->selectDB($dbname);
$this->db = $this->dbname->selectCollection($table);
} else {
$this->db = $this->mongo->$dbname->$table;
}
}
public function getVersion()
{
return MongoClient::VERSION;
}
/**
* 单例模式
* @return Mongo|null
*/
//public static function getInstance($host=null, $port=null, $dbname=null, $table=null){
//
// if(!(self::$instanceof instanceof self)){
// self::$instanceof = new self($host, $port, $dbname, $table);
// }
//
// return self::$instanceof;
/