In this paper, we describe the design mode of PHP and the implementation of single example mode. Share to everyone for your reference, specific as follows:
Design patterns simply say the solution to a particular type of problem
Factory mode: create appropriate objects for requirements
Class factory{
function __construct ($name) {
if file_exists ('./'. $name. Class.php ') {return
new $name;
} else{
die (' not exist ');}}
single case mode: creates only one instance of an object, does not allow the creation of instances, and saves resources (for example, connection to a database)
Class instance{public
$val = ten;
private static $instance;
Private Function __construct () {}
Private function __clone () {}
//Set to static method to invoke public
static function by class GetInstance () {
/*if (!isset (self:: $instance)) {
self:: $instance = new self;
} *
/if (!isset (instance:: $instance)) {
instance:: $instance = new self;
}
Return instance:: $instance;
}
}
$obj _one = Instance::getinstance ();
$obj _one->val =;
Clone can invoke __clone () to create a new object
//$obj _two = Clone $obj _one;
$obj _two = Instance::getinstance ();
echo $obj _two->val;
Echo ' <p> ';
Var_dump ($obj _one, $obj _two);
The results of the operation are as follows:
object (instance) [1] public
' val ' => int
(instance) [1] public
' val ' => int 20
Application: Database connection class (DB Access Oject)
Class mysqldb{Private $arr = Array (' Port ' => 3306, ' host ' => ' localhost ', ' username ' => ' root ',
' Passward ' => ' root ', ' dbname ' => ' instance ', ' CharSet ' => ' UTF8 ');
Private $link;
Static $instance; Private Function __clone () {} private Function __construct () {$this->link = mysql_connect ($this->arr[' host '), $t
his->arr[' username '], $this->arr[' Passward ']) or Die (Mysql_error ());
mysql_select_db ($this->arr[' dbname ']) or Die (' db error ');
Mysql_set_charset ($this->arr[' CharSet ']);
The static public function getinsance () {if (!isset (mysqldb:: $instance)) {mysqldb:: $instance = new Self;
Return MYSQLDB:: $instance;
Public Function Query ($sql) {if ($res = mysql_query ($sql)) {return $res;
}return false;
}//fetch one public function Get_one ($sql) {$res = $this->query ($sql);
if ($result = Mysql_fetch_row ($res)) {return $result [0]; }}//fetch row
Public Function Get_row ($sql) {$res = $this->query ($sql);
if ($result = Mysql_fetch_assoc ($res)) {return $result;
return false;
//fetch All public Function Get_all ($sql) {$res = $this->query ($sql);
$arr = Array ();
while ($result = Mysql_fetch_assoc ($res)) {$arr [] = $result;
return $arr;
}} $mysql = Mysqldb::getinsance ();
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.