您好,欢迎访问一九零五行业门户网

php框架练习

框架的作用: 功能:操作数据库 模版引擎smarty 分析,应该具备什么功能? a:具体配置文件 ,配置文件的读取功能 b:数据库处理类 c:目录结构 先建一个includes文件放入,配置文件(config.php),配置类(conf.class.php),mysql数据库处理类(mysql.class.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;
其它类似信息

推荐信息