PHP Design Pattern Series-Template mode _php tutorial

Source: Internet
Author: User
Tags mysql connect pconnect
Template mode
The template design pattern creates an abstract object that implements a set of methods that the subclass typically uses as a template for its own design.

Design Scenarios
Typically used for database abstraction classes.
Code Design:
[PHP]
if (!defined (' is_initphp ')) exit (' Access denied! ');
/*********************************************************************************
* initphp 2.0 domestic PHP Development Framework Dao-dbbase Driver DB base class
*-------------------------------------------------------------------------------
* All rights reserved: Copyright by initphp.com
* You are free to use the source code, but in the course of use, please keep the author information. Respect for the fruits of others ' work is respect for themselves
*-------------------------------------------------------------------------------
* $Author: Zhuli
* $Dtime: 2011-10-09
***********************************************************************************/
Abstract class dbbaseinit{

/**
* Abstract Database Links
* @param string $host SQL Server
* @param string $user database user name
* @param string $password Database login Password
* @param string $database Database
* @param string $charset encoding
* @param string $pconnect whether persistent linking
*/
Abstract protected function Connect ($host, $user, $password, $database, $charset = ' UTF8 ', $pconnect = 0);

/**
* Abstract Database Execution statement
* @param string $sql SQL statement
* @return obj
*/
Abstract protected function query ($sql);

/**
* Abstract Database-number of rows in the result set
* @param $result result set
* @return Array
*/
Abstract protected function result ($result, $num =1);

/**
* Abstract Database-get a row from the result set as an associative array
* @param $result result set
* @return Array
*/
Abstract protected function Fetch_assoc ($result);

/**
* Abstract Database-gets column information from the result set and returns as an object
* @param $result result set
* @return Array
*/
Abstract protected function Fetch_fields ($result);

/**
* Abstract Database-number of records affected by previous operation
* @return int
*/
Abstract protected function affected_rows ();

/**
* Abstract Database-number of rows in the result set
* @param $result result set
* @return int
*/
Abstract protected function num_rows ($result);

/**
* Abstract Database-Number of fields in the result set
* @param $result result set
* @return int
*/
Abstract protected function Num_fields ($result);

/**
* Abstract database-Gets the ID value of the previous insert
* @return Int
*/
Abstract protected function insert_id ();

/**
* Abstract Database-free result memory
* @param obj $result objects that need to be disposed
*/
Abstract protected function Free_result ($result);

/**
* Abstract Database Link close
* @param string $sql SQL statement
* @return obj
*/
Abstract protected function close ();

/**
* Error message
* @return String
*/
Abstract protected function error ();
}


if (!defined (' is_initphp ')) exit (' Access denied! ');
/*********************************************************************************
* initphp 2.0 domestic PHP Development Framework Dao-mysqli base class
*-------------------------------------------------------------------------------
* All rights reserved: Copyright by initphp.com
* You are free to use the source code, but in the course of use, please keep the author information. Respect for the fruits of others ' work is respect for themselves
*-------------------------------------------------------------------------------
* $Author: Zhuli
* $Dtime: 2011-10-09
***********************************************************************************/
Class Mysqliinit extends dbbaseinit{

Public $link _id; Linked objects

/**
* MySQL Connector
* @param string $host SQL Server
* @param string $user database user name
* @param string $password Database login Password
* @param string $database Database
* @param string $charset encoding
* @param string $pconnect whether persistent linking
* @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 Actuator
* @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 result set
* @return Array
*/
Public function result ($result, $num =1) {
Return Mysqli_result ($result, $num);
}

/**
* Get a row from the result set as an associative array
* @param $result result set
* @return Array
*/
Public Function Fetch_assoc ($result) {
Return Mysqli_fetch_assoc ($result);
}

/**
* Get column information from the result set and return as an object
* @param $result result set
* @return Array
*/
Public Function Fetch_fields ($result) {
Return Mysqli_fetch_field ($result);
}

/**
* Number of rows in the result set
* @param $result result set
* @return int
*/
Public Function Num_rows ($result) {
Return Mysqli_num_rows ($result);
}

/**
* Number of fields in the result set
* @param $result result set
* @return int
*/
Public Function Num_fields ($result) {
Return Mysqli_num_fields ($result);
}

/**
* Release result memory
* @param obj $result objects that need to be disposed
*/
Public Function Free_result ($result) {
Return Mysqli_free_result ($result);
}

/**
* Gets the ID value of the previous insert
* @return Int
*/
Public Function insert_id () {
Return mysqli_insert_id ($this->link_id);
}

/**
* The number of records affected by the previous operation
* @return int
*/
Public Function affected_rows () {
Return Mysqli_affected_rows ($this->link_id);
}

/**
* Close 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

http://www.bkjia.com/PHPjc/478081.html www.bkjia.com true http://www.bkjia.com/PHPjc/478081.html techarticle The template pattern template design pattern creates an abstract object that implements a set of methods that the subclass typically uses as a template for its own design. Design scenarios are typically used in databases ...

  • 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.