Php Framework exercises

Source: Internet
Author: User
Tags php framework
Role of the framework: function: What functions should I have to perform smarty analysis on the database template engine? A: For specific configuration files, the reading function of the configuration file B: database processing class c: directory structure first, create a pair file and put it into the configuration file (config. php), configuration class (conf. class. php), mysql database processing class (mysql. class. php ),

Role of the framework: function: What functions should I have to perform smarty analysis on the database template engine? A: For specific configuration files, the reading function of the configuration file B: database processing class c: directory structure first, create a pair file and put it into the configuration file (config. php), configuration class (conf. class. php), mysql database processing class (mysql. class. php ),

Role of the framework:

Function: operates the database template engine smarty.

What functions should I have for analysis?

A: The Configuration File Reading function.
B: database processing
C: directory structure

First, create a pair of des files and put them into the configuration file (config. php), configuration class (conf. class. php), mysql database processing class (mysql. class. php), website initialization file (init. php)

Config. php file:

// The website configuration file $ _ cfg = array (); $ _ cfg ['host'] = 'localhost'; $ _ cfg ['user'] = 'root '; $ _ cfg ['pwd'] = 'root'; $ _ cfg ['db'] = 'xxzdb'; $ _ cfg ['Char '] = 'utf8 '; // $ _ cfg [''] =''; // $ _ cfg [''] = '';


Conf. class. php

// Read the configuration file class // function: Read config. php returns the value of a configuration option class Conf {private static $ ins = false; private $ info = array (); final protected function _ construct () {require (ROOT. 'des/config. php '); $ this-> info = $ _ cfg;} // 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 '); // The processing class of the database // The abstract class has no method. 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 My SQL 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;} // create a connection to 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 the database protected function select_db ($ dbname = '') {if ($ dbname = '') {$ SQL = 'use '. $ this-> conf-> db; $ this-> query ($ SQL) ;}// set the character set protected function setChar () {$ SQL = 'set names '. $ this-> conf-> char; $ this-> query ($ SQL);} // execute the SQL statement public function query ($ SQL) {return mysql_query ($ SQL, $ this-> conn);} // retrieve all rows public function getAll ($ SQL) {$ rs = $ this-> query ($ SQL ); $ list = array (); while ($ row = mysql_fetch_assoc ($ rs) {$ list [] = $ row;} return $ list ;} // retrieve a row 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 public function _ destruct () {}}/* $ db = Mysql: getIns (); print_r ($ db); $ SQL = "select * from art limit 5 "; print_r ($ db-> getAll ($ SQL ));*/


Init. php

// Initialize the website FILE/** magic method _ FILE _ the current absolute path FILE function: is responsible for introducing the ROOT directory of the current website, so all files required on the page * // ROOT represents the website's follow-up path define ('root', str_replace ('\\','/', str_replace ('replace des \ init. php ', '',__ FILE _); require (ROOT. 'des/conf. class. php '); require (ROOT. 'des/mysql. class. php '); // echo ROOT;
Related Article

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.