has been studying PHP design patterns, but did not experience the use of, so or smattering, through a few days of learning finally to PHP's singleton design mode a little bit of understanding, hereby write a database processing class (only involves the simple principle), so that they can be easily consulted in the future, as for other design patterns I will slowly understand , and then publish it as an instance to the blog:
/** * Single case mode*/classdb{Private Static $mysqli;//The only instance in the class Private function__construct () {}//prevent external instantiation of the class Private function__clone () {}//prohibit the instantiation of this class by copying Public Static functionConnect () {//Database Connection MethodsSelf::$mysqli=NewMysqli ("localhost", "root", "" "," test "); Self::$mysqli->query ("Set Names UTF8"); } Public Static functionSelect$table){//database operation methods, follow-up can continue to improve and add ...Self::Connect (); $result= Self::$mysqli->query ("SELECT * from".)$table); $result _arr=Array(); while($query=$result-Fetch_assoc ()) { $result _arr[] =$query; } return $result _arr;//The result set is returned as an array }}$result= Db::select ("article");Echo' <pre> ';Print_r($result);Echo' </pre> ';
PHP design mode example of a single instance (Design mysqli data processing Class)