In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what design patterns does PHP have". In daily operation, I believe many people have doubts about what design patterns PHP has. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what design patterns does PHP have?" Next, please follow the editor to study!
There are many design patterns. Here we introduce four patterns: singleton pattern, factory pattern, combination pattern and strategy pattern.
/ * * singleton mode * @ author YangYang * can be thought of as generating only one object of this class in one http request (that is, only new classname once) * the classic example is database connection (redis,mongodb,memcache, etc.) * We may need to add, delete, modify and check multiple sql operations to the database in one http request * but if each sql is executed in a http request, we mysql_connect () It will obviously lead to a waste of server resources * in order to save resources, you can use singleton mode to realize that a http request can be made only once mysql_connect () * that is, put mysql_connect () in the _ _ construct of the class method, and make the _ _ construct method private. * in this way, you can only obtain the resource connector of mysql_connect () through the getInstance () method * getInstance () method to determine whether the myql connector already exists. If there is, return the connector directly * otherwise new classname () calls the _ _ construct method to execute mysql_connect () to get the resource connector and return the connector * because now PHP no longer recommends using mysql function for database operation directly, but recommends database operation through PDO, so write a singleton mode of simple PDO connection here only 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 run result: the id=1 record in the data 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, prevent cloning private function _ _ clone () {} / / to obtain connection instance public static function getInstance ($dsn,$dbUser,$dbPassword) {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) is called many times, but it is still the same pdo object print_r ($result); at this point, the study of "what are the design patterns of PHP" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.