Template Mode
The template design pattern creates an abstract object that implements a set of methods. subclasses usually use this object as a template for their own design.
Design scenario
It is generally used for database abstract classes.
Code Design:
[Php]
<? Php
If (! Defined ('is _ initphp') exit ('Access Denied! ');
/*************************************** **************************************** **
* InitPHP 2.0 domestic PHP development framework Dao-dbbase Driver DB base class
*-------------------------------------------------------------------------------
* CopyRight: CopyRight By initphp.com
* You can use the source code freely, but keep the author information during use. Respecting others means respecting yourself.
*-------------------------------------------------------------------------------
* $ Author: zhuli
* $ Dtime: 2011-10-09
**************************************** **************************************** ***/
Abstract class dbbaseInit {
/**
* Abstract Database Link
* @ Param string $ host SQL Server
* @ Param string $ user database username
* @ Param string $ password Database logon password
* @ Param string $ database
* @ Param string $ charset Encoding
* @ Param string $ pconnect persistent link
*/
Abstract protected function connect ($ host, $ user, $ password, $ database, $ charset = 'utf8', $ pconnect = 0 );
/**
* Abstract database execution statements
* @ Param string $ SQL SQL statement
* @ Return obj
*/
Abstract protected function query ($ SQL );
/**
* Abstract database-number of rows in the result set
* @ Param $ result set
* @ Return array
*/
Abstract protected function result ($ result, $ num = 1 );
/**
* Abstract database-get a row from the result set as an associated array
* @ Param $ result set
* @ Return array
*/
Abstract protected function fetch_assoc ($ result );
/**
* Abstract database-retrieve column information from the result set and return it as an object
* @ Param $ result set
* @ Return array
*/
Abstract protected function fetch_fields ($ result );
/**
* Abstract database-number of records affected by previous operations
* @ Return int
*/
Abstract protected function affected_rows ();
/**
* Abstract database-number of rows in the result set
* @ Param $ result set
* @ Return int
*/
Abstract protected function num_rows ($ result );
/**
* Abstract database-number of fields in the result set
* @ Param $ result set
* @ Return int
*/
Abstract protected function num_fields ($ result );
/**
* Abstract database-get the ID value of the previous INSERT
* @ Return Int
*/
Abstract protected function insert_id ();
/**
* Abstract database-release result memory
* @ Param obj $ result: object to be released
*/
Abstract protected function free_result ($ result );
/**
* The abstract database link is closed.
* @ Param string $ SQL SQL statement
* @ Return obj
*/
Abstract protected function close ();
/**
* Error message
* @ Return string
*/
Abstract protected function error ();
}
<? Php
If (! Defined ('is _ initphp') exit ('Access Denied! ');
/*************************************** **************************************** **
* InitPHP 2.0 domestic PHP development framework Dao-mysqli base class
*-------------------------------------------------------------------------------
* CopyRight: CopyRight By initphp.com
* You can use the source code freely, but keep the author information during use. Respecting others means respecting yourself.
*-------------------------------------------------------------------------------
* $ Author: zhuli
* $ Dtime: 2011-10-09
**************************************** **************************************** ***/
Class mysqliInit extends dbbaseInit {
Public $ link_id; // link object
/**
* MYSQL Connector
* @ Param string $ host SQL Server
* @ Param string $ user database username
* @ Param string $ password Database logon password
* @ Param string $ database
* @ Param string $ charset Encoding
* @ Param string $ pconnect persistent link
* @ Return obj
*/
Public function connect ($ host, $ user, $ password, $ database, $ charset = 'utf8', $ pconnect = 0 ){
$ Link_id = ($ pconnect = 0 )? Mysqli_connect ($ host, $ user, $ password): mysqli_pconnect ($ host, $ user, $ password );
If (! $ Link_id) InitPHP: initError ('mysql connect error! ');
Mysqli_query ($ link_id, 'set names'. $ charset );
If (! Mysqli_select_db ($ link_id, $ database) InitPHP: initError ('database is not exist! ');
Return $ link_id;
}
/**
* SQL executor
* @ Param string $ SQL SQL statement
* @ Return obj
*/
Public function query ($ SQL ){
Return mysqli_query ($ this-> link_id, $ SQL );
}
/**
* Number of rows in the result set
* @ Param $ result set
* @ Return array
*/
Public function result ($ result, $ num = 1 ){
Return mysqli_result ($ result, $ num );
}
/**
* Get a row from the result set as an associated array
* @ Param $ result set
* @ Return array
*/
Public function fetch_assoc ($ result ){
Return mysqli_fetch_assoc ($ result );
}
/**
* Retrieve column information from the result set and return it as an object
* @ Param $ result set
* @ Return array
*/
Public function fetch_fields ($ result ){
Return mysqli_fetch_field ($ result );
}
/**
* Number of rows in the result set
* @ Param $ result set
* @ Return int
*/
Public function num_rows ($ result ){
Return mysqli_num_rows ($ result );
}
/**
* Number of fields in the result set
* @ Param $ result set
* @ Return int
*/
Public function num_fields ($ result ){
Return mysqli_num_fields ($ result );
}
/**
* Release the result memory.
* @ Param obj $ result: object to be released
*/
Public function free_result ($ result ){
Return mysqli_free_result ($ result );
}
/**
* Obtain the ID value of the previous INSERT statement.
* @ Return Int
*/
Public function insert_id (){
Return mysqli_insert_id ($ this-> link_id );
}
/**
* Number of records affected by the previous operation
* @ Return int
*/
Public function affected_rows (){
Return mysqli_affected_rows ($ this-> link_id );
}
/**
* Close the connection.
* @ Return bool
*/
Public function close (){
If ($ this-> link_id! = NULL) @ mysqli_close ($ this-> link_id );
$ This-> link_id = NULL;
Return true;
}
/**
* Error message
* @ Return string
*/
Public function error (){
Return mysqli_error ($ this-> link_id );
}
}
Author: initphp