PHP design mode example of a single instance (Design mysqli data processing Class)

Source: Internet
Author: User

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)

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.