The design mode of the classic PHP interview questions (frequently encountered) and the php design mode. The design mode of the classic PHP interview questions (frequently encountered), the design mode of the php design mode is often mentioned in the interview process, sometimes we will give examples to illustrate the application scenarios of various design modes: the design mode of the PHP classic interview questions (often encountered), the php design mode
The design pattern is often mentioned during the interview process. 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 );
Workshop (often encountered), php design pattern often mentioned during the interview process, sometimes let us illustrate the application scenarios of various design patterns...