PHP classic interview question design model (frequently encountered) _ php instance

Source: Internet
Author: User
There are many design patterns in php, but the design patterns are often mentioned in php interview questions. This article mainly introduces you to the design patterns of php classic interview questions, if you need a friend, let's take a look at the design pattern, which is often mentioned during the interview. Sometimes, we will give examples to illustrate the application scenarios of various design patterns.

Using design patterns can reduce our workload and optimize our code.

There are many design patterns. Here we will introduce the singleton mode, factory mode, combination mode, and Policy mode.

If you have any code problems or have a better way, please let us know. Thank you !!!!!

/*** Singleton mode ** @ author YangYang <1812271619@qq.com> * you can think of generating only one object of this class in an http request (that is, only new classname once) * a typical example is database connection (such as redis, mongodb, and memcache) * In an http request, we may need to add, delete, modify, and query Multiple SQL operations on the database * However, if every SQL statement executed in an http request is mysql_connect (), obviously, this will lead to a waste of server resources * to save resources, you can achieve only one mysql_connect () * mysql_connect () for an http request in singleton mode () put it in _ construct of the class method, and make the _ construct Method private. * In this way, you can only use the getInstance () method to obtain the resource connector * getInstance () of mysql_connect () method to Determine whether a myql connector exists. If yes, the connector * is returned directly. Otherwise, the new classname () method is called and the _ construct method is executed. mysql_connect () to obtain the resource connector, and return the connector * Because PHP does not recommend that you use the mysql function for database operations now, instead, we recommend that you use PDO for database operations, so here is a simple PDO connection Singleton mode * here is just to explain the singleton principle, database anti-SQL injection and other issues are not considered * Preparation work database: test Data Table: user field: id name Record: 1 CodeAnti * Final running result: the record id = 1 in the table user is deleted */class SinglePDO {private static $ _ instance = null; private $ _ pdo; // Private to prevent external direct instantiation of new SinglePDO (...) private function _ construct ($ dsn, $ dbUser, $ dbPassword) {try {$ this-> _ pdo = new PDO ($ dsn, $ dbUser, $ dbPassword ); $ this-> _ pdo-> exec ('set names utf8');} catch (PDOException $ e) {die ("Error :{$ e-> getMessage ()} ") ;}}// private to prevent cloning private function _ clone () {}// obtain the public static function getInstance ($ dsn, $ dbUser, $ dbPassword) of the connected instance) {if (self ::$ _ instance === null) self ::$ _ instance = new self ($ dsn, $ dbUser, $ dbPassword); return self :: $ _ instance;} // Execute SQL public function execSql ($ SQL) {$ result = $ this-> _ pdo-> exec ($ SQL); return $ result ;}} $ dsn = "mysql: host = localhost; dbname = test"; $ dbUser = "root"; $ dbPassword = ""; $ SQL = "delete from user where id = 1"; $ pdo = SinglePDO: getInstance ($ dsn, $ dbUser, $ dbPassword ); $ result = $ pdo-> execSql ($ SQL); // $ pdo-> execSql ($ SQL) multiple calls, but it is still the same pdo object print_r ($ result );

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.