閱讀864 返回首頁    go 阿裏雲 go 技術社區[雲棲]


php框架練習

框架的作用:

功能:操作數據庫 模版引擎smarty

分析,應該具備什麼功能?

a:具體配置文件 ,配置文件的讀取功能
b:數據庫處理類
c:目錄結構

先建一個includes文件放入,配置文件(config.php),配置類(conf.class.php),mysql數據庫處理類(mysql.class.php),網站初始化文件(init.php)

config.php  文件:

 

//網站的配置文件

$_cfg=array();

$_cfg['host']='localhost';
$_cfg['user']='root';
$_cfg['pwd']='root';
$_cfg['db']='xxzdb';
$_cfg['char']='utf8';
//$_cfg['']='';
//$_cfg['']='';


conf.class.php

 

//配置文件的讀取類
//作用:讀取config.php 並能返回某個配置選項的值
class Conf{
	private static $ins = false;
	private $info = array();
	
	
	final protected function __construct(){
		require(ROOT .'includes/config.php');
		$this->info = $_cfg;
	} 
	
	//單例
	public static function getIns(){
		if(self::$ins === false){
			self::$ins = new self();
		
		}
		return self::$ins;
		
	}
	
	//讀取配置文件的信息
	public function __get($key){
		if(array_key_exists($key,$this->info)){
		return $this->info[$key];
		} else {
		return null;
		}
	}

	public function __set($key,$value){
		$this->info[$key] = $value;
	
	}
	//測試方法
	public function printc(){
	
		print_r($this->info);
	}
}

//調用
/*
$conf = Conf::getIns();

$conf->template_dir='d:/www';
echo $conf->printc();
*/

 

mysql.class.php

//require('conf.class.php');
//數據庫的處理類

//抽象類 沒有方法體
abstract class abs_db{
	abstract protected function connect();
	abstract protected function select_db($dbname='');
	abstract protected function setChar();
	abstract protected function	query($sql);
	abstract protected function	getAll($sql);
	abstract protected function	getRow($sql);
	abstract protected function getOne($sql);
	abstract protected function error();
}	

class Mysql extends abs_db{
	private static $ins = false;
	private $conn = false;
	private $conf = false;
	
	protected function __construct(){
		
		$this->conf = Conf::getIns();
	    $this->connect();
		$this->select_db();
		$this->setChar();
	}
	
	public static function getIns(){
		if(self::$ins === false){
			self::$ins= new self();
		}
		return self::$ins;
	}
	
	//創建連接
	protected function connect(){
		$this->conn = mysql_connect($this->conf->host,$this->conf->user,$this->conf->pwd);
		if(!$this->conn){
			$err = new Exception('連接失敗');
			throw $err;
		}
	}
	
	//選擇數據庫
	protected function select_db($dbname=''){
		if($dbname == ''){
			$sql='use '.$this->conf->db;
			$this->query($sql);
		}
	}
	
	//設置字符集
	protected function setChar(){
		$sql='set names '.$this->conf->char;
		$this->query($sql);
	}
	
	//執行sql語句
	public function query($sql){
		return mysql_query($sql,$this->conn);
	}
	
	//取回所有行
	public function getAll($sql){
		$rs = $this->query($sql);
		
		$list=array();
		while($row = mysql_fetch_assoc($rs)){
			$list[] = $row;
		}
		return $list;
	}
	
	//取回一行
	public function getRow($sql){
		$rs = $this->query($sql);
		return mysql_fetch_assoc($rs);
	}
	
	public function getOne($sql){
	
	}
	
	public function error(){
		print_r(mysql_error($this->conn));
	}
	
	//關閉資源
	public function __destruct(){
	}
}
/*
$db = Mysql::getIns();

print_r($db);
$sql="select * from art limit 5";
print_r($db->getAll($sql));
*/


init.php

//網站的初始化文件
/**

魔術方法
__FILE__ 當前絕對路徑


文件作用:
負責當前網站的根目錄
引入所以頁麵都需要的文件

*/

//ROOT 代表網站的跟路徑
define('ROOT',str_replace('\\','/',str_replace('includes\init.php','',__FILE__)));

require(ROOT .'includes/conf.class.php');
require(ROOT .'includes/mysql.class.php');
//echo ROOT;



 

 

最後更新:2017-04-02 22:15:46

  上一篇:go Intel至強Phi 5110P集群躋身綠色500強名單
  下一篇:go 那些年,我們一起追的Android