php设计模式之单例模式

<?php

namespace Tools;

/*单例模式
单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
*/

//一个数据操作类
class Database{
	private $_where;
	private $_order;
	private $_limit;

	//静态变量保存全局实例
	private static $_instance = null;

	//私有构造函数,防止外界实例化对象
	private  function __construct()
	{
	}

	//单例模式实例对象(只实例化一次)
	static function getInstance(){
		if(is_null(self::$_instance) || isset($_instance)){
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	function where($where){
		$this->_where = $where;
		return $this;
	}

	function order($order){
		$this->_order = $order;
		return $this;
	}

	function  limit($limit){
		$this->_limit = $limit;
		return $this;
	}
}


//单例模式实例化
$db =\Tools\Database::getInstance();
var_dump($db);



你可能感兴趣的:(php设计模式之单例模式)