PHP Framework Exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.