您的位置:首页 > 编程语言 > PHP开发

PHP编程——我的第一个框架(初步设计)

2014-04-04 13:42 411 查看
之前使用过thinkphp的框架开发网站,而在自己学习了面向对象编程之后,也希望能自己开发一个框架把MVC分层,减少代码量。

先简单介绍我的框架结构:

nyframework/
├── css/
│   ├── bootstrap.css
│   └── bootstrap.min.css
├── config.php
├── Control.class.php
├── Model.class.php
├── Query.class.php
├── test.php
└── View.class.php


文件简介:

config.php文件:基本配置文件,设置数据库服务器相关数据
Control.class.php:控制类,负责程序逻辑控制
Model.class.php:模型类,负责结构化数据
Query.class.php:查询类,负责数据库连接及对数据表的CURD操作
View.class.php:视图类,负责页面显示

1.config.php文件源码

<?
$configure = Array(
'DATABASE_TYPE'=>'',
'SERVER_NAME'=>'l',
'SERVER_USER'=>'',
'SERVER_PASSWORD'=>''
);


2.控制类Control.class.php文件源码

<?
include_once("Query.class.php");
include_once("Model.class.php");
include_once("View.class.php");

class Control{

private $queryObject;
private $modelObject;
private $viewObject;

/*
*构造函数
*创建查询对象,构造控制类
*/
function __construct($config)
{
$this->queryObject = new Query($config['SERVER_NAME'],$config['SERVER_USER'],$config['SERVER_PASSWORD']);
$this->modelObject = new Model();
$this->viewObject = new View();
echo "<div>Control object construct!</div>";

}
/*
*析构函数
*销毁查询对象
*/
function __destruct()
{
unset($this->queryObject);
unset($this->modelObject);
unset($this->viewObject);
echo "<div>Control object destruct!</div>";

}

/*
*设置查询对象方法
*初始化查询对象
*/
function setQueryObject($db,$tab)
{
$this->queryObject->setDB($db);
$this->queryObject->setTable($tab);
}

/*
*获得查询对象方法
*模型化查询数据
*/
function select($db,$tab)
{
if($db!="" && $tab!="")
{
$this->setQueryObject($db,$tab);
$data = $this->queryObject->selectAll();
$this->modelObject->initialData($data);
}
}

function disp($style)
{
$this->viewObject->setPageSize(10);
$this->viewObject->setStyle($style);
$this->viewObject->displayTable($this->modelObject->getData());
}

}

?>


3.模型类Model.class.php文件源码:

<?
class Model{

private $dataArray;//查询数据数组
function __construct()
{
echo "<div>Model Object construct!</div>";

}
function __destruct()
{
echo "<div>Model Object destruct!</div>";
}
//初始化数组
function initialData($data)
{
$this->dataArray = $data;
}
//
function getData()
{
return $this->dataArray;
}

}

?>


4.查询类Query.class.php文件源码

<?

class Query{
/************************************
*数据库、数据表、连接成员
*************************************/
private $serverName;//服务器名
private $userName;//服务器用户名
private $userPassword;//服务器用户密码
private $databaseName;//数据库名
private $tableName;//数据表名
private $connect;//连接数据库属性
/************************************
*数据表信息成员
*************************************/
private $data;//查询返回数组
private $condition;//查询条件
private $pageSize;//查询页面大小
private $currentPage;//查询当前页码
private $firstId;//查询页首条记录
private $sortTable;//排序条件
private $colArray;//字段名数组
private $colTotal;//字段名总数
private $totalNum;//记录总条数
private $totalPage;//总页数
/*
*构造函数
*初始化数据库连接成员,创建数据库连接
*/
function __construct($serverName,$userName,$userPassword)
{
$this->serverName = $serverName;
$this->userName = $userName;
$this->userPassword = $userPassword;
$this->connect = mysql_connect($this->serverName,$this->userName,$this->userPassword) or die("数据库连接出错!".mysql_error());
echo "<div>Query object construct!</div>";

}
function __destruct()
{
mysql_close();
echo "<div>Query object destruct!</div>";
}
/***********************************************
*设置数据库查询语句相关方法
*初始化数据库成员
************************************************/
/*
*连接数据库,构造函数
*无返回值
*/
function setDB($db)
{
$this->databaseName = $db;
if(!$this->connect)
{
die("数据库连接出错!".mysql_error());
}
else
{
mysql_select_db($this->databaseName,$this->connect) or die("选择数据库出错".mysql_error());
}
//echo "<div>".($this->databaseName)."</div>";
//echo "<div>".($this->databaseName)."</div>";
}

/*
*设置查询数据表方法
*无返回值
*/
function setTable($tableName)
{
$this->tableName = $tableName;
}

/*
*设置查询条件方法
*无返回值
*/
function setCondition()
{
$this->condition = "";

if($this->sortTable)
{
//查询sql的order by条件要在limit之前
$this->condition .= " ".$this->sortTable;
}
if($this->pageSize)
{
$this->condition .= " limit ".$this->firstId;
$this->condition .= ",".$this->pageSize;
}

}
/*
*执行查询语句并初始化私有成员$this->data
*返回查询的所有数据
*/
function selectAll()
{
$sql = "select * from ".$this->tableName;
//echo $this->condition;
if($this->condition != "")
{
$sql .= " ".$this->condition;
}
//echo "<div>".$sql."</div>";
$result = mysql_query($sql,$this->connect);
$i = 0;
while($row = mysql_fetch_array($result))
{
$this->data[$i] = $row;
$i++;
}
//print_r($this->data);
return $this->data;
}
}
?>


5.视图类View.class.php文件源码

<?
class View{

private $styleName;//样式名
private $pageCurNum;//当前页页码
private $pageSize;//当前页的数据数目

function __construct()
{
echo "<div>View object construct!</div>";
}
function __destruct()
{
echo "<div>View object destruct!</div>";
}
/*
*设置样式
*以便根据样式参数名改变HTML的样式
*/
function setStyle($styleName)
{
$this->styleName = $styleName;
}
function setCurNum($num)
{
$this->pageCurNum = $num;
}
function setPageSize($num)
{
$this->pageSize = $num;
}
/*
*打印数据
*
*/
public function displayTable($data)
{
echo "array length:".count($data);
echo "styleName:".$this->styleName;
//此处可以利用数组的特性对数据进行分页
echo "<table class='$this->styleName'>";
$i = 0;
foreach($data as $value)
{
echo "<tr>";
$col = ceil(count($value)/2);
for($j=0;$j<$col;$j++)
{
echo "<td>".$value[$j]."</td>";
}
echo "</tr>";
$i++;
if($i>=$this->pageSize)
{
break;
}
}

echo "</table>";
}
}
?>


6.测试页test.php文件源码

<?
header("Content-type:text/html;charset=utf-8");
include_once("Control.class.php");
include_once("config.php");
$ctrl = new Control($configure);
$db = 'test';
$tab = 'test';
$style = "table table-hover";
$ctrl->select($db,$tab);
$ctrl->disp($style);
unset($ctrl);

?>
<html>
<head>
<title>
My Framework
</title>
<link rel="stylesheet" href="css/bootstrap.css" type="text/css"/>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
</head>
<body>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: