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;



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱酥网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值