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 ...
随机推荐
- [ReadingNotes] Search the links, static final in the java
[ReadingNotes] Search the links, static final in the java */--> pre { background-color: #2f4f4f;l ...
- GitHub上整理的一些工具,求补充
http://segmentfault.com/q/1010000002404545 技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MS ...
- 2的32次方 分类: C#小技巧 2014-08-05 18:18 406人阅读 评论(0) 收藏
版权声明:本文为博主原创文章,未经博主允许不得转载.
- 听听Matt Rogish说怎么面试程序员
Google的人力运营高级副总裁Laszlo Bock在一次采访中说Google发现在面试程序员时问智力题完全是浪费时间,Matt Rogish在他的这篇博客How to Interview Prog ...
- .net 4中的pInvokeStackImbalance MDA默认是开启的
今天把我之前发的一个小工具FreeEverything(基于everything的一个简易磁盘清理工具)升级到了.net framework 4.5,并且去掉了对mvvmlight的依赖.结果在测试运 ...
- PPT扁平化风格设计手册
钱文嘉:颜色选择,搭配 http://www.pptfans.cn/341917.html
- MySql The service could not be started
MySql安装 由于需要用mySql数据库今天就把它安上了,每次安装软件,数据库总是够我们折腾的,有时出现错误甚至比重装系统还要让人头疼. 今天在安的过程中就不出了很多错误,在重启与重装的反复捣鼓中终 ...
- OfficePickers
OfficePickers http://www.codeproject.com/Articles/12327/Office-2003-Color-Picker 来自为知笔记(Wiz) 附件列 ...
- alloc、init你弄懂50%了吗?
前言 这是一篇我记录对alloc.init分析思考的笔记.如果读者想看懂我的第二个思考,可能需要您至少了解内存的分段分页管理,如果您对其一点都不知道,可以先看这篇软文简单了解一下.另外很重要的一点是, ...
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...