The role of the framework:
Function: Operation database template engine Smarty
Analysis, what should be the function?
A: Specific configuration file, configuration file read function
B: Database processing classes
C: Directory Structure
First build a includes file into, config file (config.php), config class (conf.class.php), MySQL Database processing class (mysql.class.php), Web site initialization file (init.php)
config.php file:
Site profile $_cfg=array (); $_cfg[' host ']= ' localhost '; $_cfg[' user ']= ' root '; $_cfg[' pwd ']= ' root '; $_cfg[' db ']= ' xxzdb '; $_cfg[' char ']= ' UTF8 ';//$_cfg[']= ';//$_cfg[']= ';
conf.class.php
Read class/Function of profile: Read config.php and return the value of a configuration option class Conf{private static $ins = false;private $info = Array (); final protected Fu Nction __construct () {require (ROOT. ') Includes/config.php '); $this->info = $_cfg;} A singleton public static function Getins () {if (self:: $ins = = = False) {self:: $ins = new self ();} Return self:: $ins;} Read the configuration file information 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;} Test method Public Function Printc () {print_r ($this->info);}} Call/* $conf = Conf::getins (); $conf->template_dir= ' d:/www '; echo $conf->PRINTC (); */
mysql.class.php
Require (' conf.class.php ');//Database processing class//abstract class does not have method body abstract class Abs_db{abstract protected function connect (); abstract protected function select_db ($dbname = "), abstract protected function SetChar (), abstract protected functionquery ($sql) ; abstract protected Functiongetall ($sql); abstract protected functiongetrow ($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 __co NStruct () {$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;} Create connection protected function connect () {$this->conn = mysql_connect ($this->conf->host, $this->conf->user, $this->conf->pwd); if (! $this->conn) {$err = new Exception (' Connection failed '); throw $err;}} Select Database protected function select_db ($dbname = ") {if ($dbname = = ') {$sql = ' use'. $this->conf->db; $this->query ($sql);} Sets the character set protected function SetChar () {$sql = ' set names '. $this->conf->char; $this->query ($sql);} Execute SQL statement Public Function query ($sql) {return mysql_query ($sql, $this->conn);} Retrieve all line public function GetAll ($sql) {$rs = $this->query ($sql), $list =array (), while ($row = Mysql_fetch_assoc ($rs)) {$ List[] = $row;} return $list;} Retrieve one line of 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));} Close Resource public Function __destruct () {}}/* $db = mysql::getins ();p Rint_r ($db), $sql = "SELECT * FROM Art limit 5";p Rint_r ($db- >getall ($sql)); * *
init.php
Web site initialization File/** Magic method __file__ The current absolute path file role: Responsible for the current site root directory introduced so that the page needs to file *///root on behalf of the site's path 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 Framework Exercises