PHP design mode (1)

Source: Internet
Author: User
: This article mainly introduces the PHP design mode (1). If you are interested in the PHP Tutorial, refer to it. When coding PHP programs, in order to better maintain code and understand code in the future, it is essential to use some suitable design patterns. next I will share with you the ordering example mode first, if something is wrong or inappropriate, I hope PHPer can help me to point it out.

  • Singleton mode

The object in PHP starts from the script until the script ends. Therefore, the PHP Singleton mode is only in one page (this may contain many other pages, not a narrow single page) it takes effect only when this object is used multiple times. when multiple users create a project, it is inevitable that you will encounter an object instance multiple times in a request, and will not consume unnecessary resources (data control and connection operations are very effective ), another point is to ensure that the entire script is the same object. how can this mode be implemented? there are several points to note for its implementation:

1. the first step is to define the _ construct () method as a private method, so that a new instance cannot be obtained through new. The Singleton mode cannot be instantiated externally, the word itself can be instantiated internally;

2. the _ clone () method should also be blocked to prevent cloning from outside the class.

2. define a public function getInstance () to save the private variables of the instance and obtain the private variables ().

 
* Block clone of the object
*/ 
    private function __clone(){ 
// Empty function.
}/*** Get the instance through this method to prevent multiple instantiation */public static function getInstance () {if (! (Self ::$ _ instance instanceof self) {self ::$ _ instance = new self ();} return self ::$ _ instance ;}}

The theory is unconvincing. the following example shows the specific effect difference.

  _ Link = mysqli_connect ("localhost", "root", "", "mysql");}/*** get the instance through this method, prevent multiple instantiation */public static function getInstance () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self ();} return self ::$ _ instance ;} /*** Test 1, test 1 by using Singleton mode */public static function testOne () {return self: getInstance, use Singleton mode */public static function testTwo () {return new self () ;}$ obj = array (); $ begin = microtime (true ); for ($ I = 0; $ I <100; $ I ++) {/** perform two Tests here. testOne applies the Singleton mode, testTwo does not use the Singleton mode. * Let's take a look at the resources they use and the time they consumed. * // $ obj [$ I] = Singlemodel: testOne (); $ obj [$ I] = Singlemodel: testTwo ();} echo "maximum memory usage during program running :". memory_get_peak_usage (). "bytes \ r"; echo "Running time :". floatval (microtime (true)-$ begin ). "s \ r ";

Note $ obj [$ I] = Singlemodel: testTwo (); in Singlemodel, we can get the following result.

Then, comment out $ obj [$ I] = Singlemodel: testOne (); and use the non-Singlemodel. the following result is obtained.

We can see that

100 tests Singleton mode Normal mode Common/Singleton (Times)
Memory (bytes) 143816 847376 5.89
Time (s) 0.0112519 0.2541389 22.59
5 tests
Bytes 140432 168984 1.20
S 0.0112612 0.0173110 1.54

We can see that when the number of links for a script execution is 100, the performance of the Singleton mode is nearly six times better than that of the normal mode in terms of memory usage, and the time is nearly 23 times faster, when the number of connections continues to increase, the multiples will be larger, because the memory and time consumed by the Singleton mode remain unchanged, and the non-singleton mode will keep increasing, note that the error "mysqli_connect (): (08004/1040): Trop de connexions in" will be reported when the number of links increases to a certain extent in non-singleton mode ", this means that there are too many concurrent connections. in the test, we can use the following command to view the mysql maximum connections settings. Please note that you do not know why the error is reported.

show variables like 'max_connections';

By now, if you have tested it yourself, you will find that when the number of links is small, the difference is relatively small (just as the previous request has five connections ), in fact, the number of instantiation operations in a single request is also relatively small, so it does not work, of course not, you think about it, first of all, in this way, we can avoid repeated instantiation and reduce resource consumption. second, even the 10 MS gap is useful in high-concurrency systems. We use it for many benefits.

There are so many Singleton models. I would like to talk about other design models next time. if there is anything wrong, I 'd like to leave a message or email to point it out. I am very grateful!

Send Me ~

The above introduces the PHP design mode (1), including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.