Singleton mode vs. static variable in PHP

Source: Internet
Author: User
Tags php script

In PHP, there is no static variable in the general sense. Unlike Java, C + +, the survival period of static variables in PHP is only one session per PHP, so there is no such thing as a static variable of Java or C + +.

So, in PHP, the existence of a static variable is simply passing a variable in a struct (a method or class) whose scope is within this file.
Well, look at an example.

1 functionTest () {2     Static $var= 1; 3     Echo $var+ +. '4‘;5 }6 test ();7 test ();8 test ();9 //OutPutTen //1 One //2 A //3

In the three invocations of the function test, the variable $var is present in three calls and increments by 1 each time without emptying or resetting
Therefore, it can be concluded that static variables exist in the life cycle of the current structure. In the current example, the life cycle of the test function is the current PHP script, as long as the program is not released is valid.
And in the class, the code is probably like this.

1 classA2 {3     Private Static $a= 1; 4     Private $b= 2;5      Public functionAdd ()6     {7         EchoSelf::$a+ +. '8‘;9         Echo $this->b++. 'Ten‘; One     } A } - $class 1=NewA (); - $class 1-Add (); the $class 1-Add (); - $class 2=NewA (); - $class 2-Add (); - $class 2-Add (); + //Output - //1 + //2 A //2 at //3 - //3 - //2 - //4 - //3

From the running result of the class above, we get the same result in the function.
Well, perhaps a summary is
The static variables of PHP exist permanently in the life cycle of the corresponding structure, and the values remain the same regardless of how many times the struct has been invoked or instantiated.

In fact, this is the difference between dynamic variables and static variables, see this article specifically. Dynamic variables are only valid in the class, while static variables are in the current PHP script.

And look at the singleton pattern.

1 classA2 {3     Private Static $instance=NULL;4     Private $b= 1;5      Public Static functionget_instance ()6     {7         if(Self::$instance==NULL){8             $classname=__class__;9Self::$instance=New $classname(); Ten         } One         returnSelf::$instance; A     } -      Public functionAdd () -     { the         $this->b++; -     } -      Public functionShow () -     { +         Echo $this-b; -     } + } A $a= A::get_instance (); at $b= A::get_instance (); - //Here a $ A and $b variables are exactly the same!  - $a-Add (); - $a-Show (); - Echo‘ -‘; in $b-Show (); - //Output to //2 + //2

At this point, because singleton mode exists, so that $ A and $b are exactly the same object, so if you need to share the data, there is no need for static variables (nonsense, it is their own.) Because at any time, only one instance of this class exists in the application! No matter how many times you invoke the singleton, the data inside will not be re-instantiated. )

Therefore, in the singleton mode, static variables simply do not have the meaning of existence. Of course, if you have nothing to do, it is OK to use the new method to initialize the object, at which point the singleton mode is broken and returned to a state without singleton mode.

If you want to prevent the object from being instantiated with new, consider setting the __construct function of the class to the private property

1 classA2 {3     Private Static $instance=NULL;4     Private $b= 1;5     Private function__construct ()6     {7     //Code in this function8 //could not being get out of the class9     }Ten      Public Static functionget_instance () One     { A         if(Self::$instance==NULL){ -             $classname=__class__; -Self::$instance=New $classname(); the         } -         returnSelf::$instance; -     } -      Public functionAdd () +     { -         $this->b++; +     } A      Public functionShow () at     { -         Echo $this-b; -     } - } - $a= A::get_instance (); - $b= A::get_instance (); in //Here a $ A and $b variables are exactly the same!  - $a-Add (); to $a-Show (); + Echo‘ -‘; the $b-Show (); * //Output $ //2Panax Notoginseng //2 -    the //If you try to instantiate with new + $c=NewA (); A //Output the //fatal Error:call to Private a::__construct () from invalid context in + //If an instantiated object of Class A is required, it can only be initialized by an open get_instance static method

Pros: Singleton mode avoids a large number of new operations, because each new operation consumes memory resources and system resources

Disadvantage: In PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, will re-establish a new object, will be emptied after the page executes, so it seems that PHP singleton mode is meaningless, So PHP singleton mode I think it makes sense to just have multiple scenarios for a single page-level request and need to share the same object resource.

Article Source: http://blog.chinaunix.net/uid-25979788-id-3220510.html

Singleton mode vs. static variable in PHP

Related Article

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.