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;
php框架练习的更多相关文章
- 避免重复造轮子的UI自动化测试框架开发
一懒起来就好久没更新文章了,其实懒也还是因为忙,今年上半年的加班赶上了去年一年的加班,加班不息啊,好了吐槽完就写写一直打算继续的自动化开发 目前各种UI测试框架层出不穷,但是万变不离其宗,驱动PC浏览 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 旺财速啃H5框架之Bootstrap(五)
在上一篇<<旺财速啃H5框架之Bootstrap(四)>>做了基本的框架,<<旺财速啃H5框架之Bootstrap(二)>>篇里也大体认识了bootst ...
- Angular企业级开发(5)-项目框架搭建
1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...
- Scrapy框架爬虫初探——中关村在线手机参数数据爬取
关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...
- 制作类似ThinkPHP框架中的PATHINFO模式功能
一.PATHINFO功能简述 搞PHP的都知道ThinkPHP是一个免费开源的轻量级PHP框架,虽说轻量但它的功能却很强大.这也是我接触学习的第一个框架.TP框架中的URL默认模式即是PathInfo ...
- 旺财速啃H5框架之Bootstrap(四)
上一篇<<旺财速啃H5框架之Bootstrap(三)>>已经把导航做了,接下来搭建内容框架.... 对于不规整的网页,要做成自适应就有点玩大了.... 例如下面这种版式的页面. ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
- [虾扯蛋] android界面框架-Window
从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...
随机推荐
- JavaScript高级程序设计8.pdf
基本包装类型 为了便于操作基本类型值,ECMAScript定义了3个特殊的引用类型Boolean,Number和String.这些类型与本章介绍的其他用类型相似,同时也具备与各自的基本类型相应的特殊行 ...
- hdoj 1599 find the mincost route【floyd+最小环】
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PHP用ajia代码写三级联动下拉
下面是我做三级联动下拉的步骤以及逻辑 第一步:先做一个省市区表格 第二步:建个PHP页面显示用我是在<body>里放<div>用来接收要显示的省市区表格信息,里面嵌入jquer ...
- thinkphp xml编码函数
/** * XML编码 * @param mixed $data 数据 * @param string $root 根节点名 * @param string $item 数字索引的子节点名 * @pa ...
- bzoj2658: [Zjoi2012]小蓝的好友(mrx)
太神辣 treap的随机键值竟然能派上用场.. 要用不旋转的treap来进行维护区间信息 #include<cstdio> #include<cstring> #include ...
- 触发器记录表某一个字段数据变化的日志 包括插入insert 修改update 删除delete 操作
本文参考:http://www.cnblogs.com/lyhabc/articles/3236985.html ,), ), ), ), ...
- Androidannotation使用之@Rest与server交互的JSON数据转换(二)
开篇 之前的一篇博客:Androidannotation使用之@Rest获取资源及用户登录验证(一):http://blog.csdn.net/nupt123456789/article/detail ...
- sql语句游标的写法
当循环查找一张表的信息时,我们得写一张游标来对每条信息进行操作,具体格式如下 DECLARE @fitemid int DECLARE #point_cursor CURSORFORSELECT fi ...
- 我的博客css得到别人的认可
你好 在吗?? 本消息来自QQ临时会话,如果您收到骚扰信息,可点击以下网址中的"停用服务"关闭临时会话: http://shang.qq.com/widget/set.php 20 ...
- datagrid后台给每列添加js方法
protected void dgExhList_ItemDataBound(object sender, DataGridItemEventArgs e) { string param = &quo ...