In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about the principle and application of PHP single-element design pattern. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The PHP language is very powerful and has a wide range of English coverage, so many programmers have begun to use this language to develop their own websites. Here we are going to introduce you to a pattern in the following PHP language: the PHP single element design pattern. I hope it will be helpful to all of you.
Some application resources are exclusive because there is and only one resource of this type. For example, the connection to the database through the database handle is exclusive. You want to share database handles in your application because it is an overhead when keeping the connection open or closed, especially when getting a single page. The PHP single element design pattern can meet this requirement. If the application contains only one object at a time, the object is a single element (Singleton). The code in listing 1 shows a single database connection element in PHP V5.
PHP single element design pattern case:
Listing 1. Singleton.php
Php require_once ("DB.php"); class DatabaseConnection {public static function get () {static $db = null; if ($db = = null) $db = new DatabaseConnection (); return $db;} private $_ handle = null; private function _ construct () {$dsn = 'mysql://root:password@localhost/photos'; $this- > _ handle = & DB::Connect ($dsn, array ());} public function handle () {return $this- > _ handle }} print ("Handle =" .DatabaseConnection:: get ()-> handle (). "\ n"); print ("Handle =" .DatabaseConnection:: get ()-> handle (). "\ n");? >
This code displays a single class named DatabaseConnection. You cannot create your own DatabaseConnection because the constructor is dedicated. But using the static get method, you can get one and only one DatabaseConnection object. The database handle returned by the handle method is the same between the two calls, which is the proof of *. You can observe this by running the code on the command line.
% php singleton.php Handle = Object id # 3 Handle = Object id # 3%
The two handles returned are the same object. If you use a database connection single element throughout the application of the PHP single element design pattern, you can reuse the same handle anywhere. You can use global variables to store database handles, but this method applies only to smaller applications. In larger applications, you should avoid using global variables and use objects and methods to access resources.
The above is the principle and application of PHP single-element design pattern. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.