Check whether the class instantiation method can implement the "Singleton mode "?

Source: Internet
Author: User
For example, I have A class named A (this class itself is not implemented in the singleton mode, as shown in the following illustration): {code ...} there is also a global function: {code ...} then I want to call the instance of Class A elsewhere, and may need to call the instance multiple times in one request, through getObj... for example, I have A class named A (this class itself is not implemented in the singleton mode. See the method for instantiating this class later ):

Class A () {public $ str = 'this is the properties'; public $ str2 = 'properties'; private $ str3 = 'private properties'; public function _ construct ($, $ B, $ c) {// constructor} public function func1 () {// class method} public function func2 () {// class method }}

There is also a global function:

Function getObj () {require PATH. 'A. class. php '; // introduce the static $ instance file of class A defined above; // define the static variable if ($ instance) {return $ instance ;} $ instance = new A ('1', '2', '3'); return $ instance ;}

Then, I want to call the instance of Class A elsewhere, and may need to call the instance multiple times in one request and get the instance of this class through the getObj method, because it is the static variable returned, in fact, after the instance of this class is created for the first time, all subsequent calls are directly using this static variable instance, and no new instance is created. Isn't this single-instance mode ?? If not, what is the main difference ?? Thank you!

Reply content:

For example, I have A class named A (this class itself is not implemented in the singleton mode. See the method for instantiating this class later ):

Class A () {public $ str = 'this is the properties'; public $ str2 = 'properties'; private $ str3 = 'private properties'; public function _ construct ($, $ B, $ c) {// constructor} public function func1 () {// class method} public function func2 () {// class method }}

There is also a global function:

Function getObj () {require PATH. 'A. class. php '; // introduce the static $ instance file of class A defined above; // define the static variable if ($ instance) {return $ instance ;} $ instance = new A ('1', '2', '3'); return $ instance ;}

Then, I want to call the instance of Class A elsewhere, and may need to call the instance multiple times in one request and get the instance of this class through the getObj method, because it is the static variable returned, in fact, after the instance of this class is created for the first time, all subsequent calls are directly using this static variable instance, and no new instance is created. Isn't this single-instance mode ?? If not, what is the main difference ?? Thank you!

Next we will go through the PHP7 era, and your code will still look like 5. in the early days of x, PHP Code itself should be able to run smoothly. From a broad definition, you can barely calculate a single instance, but the overall feeling is very strange. As mentioned in other answers, if others do not know the getObj function, they are likely to be new by themselves. Your function for getting objects cannot constrain others.

Since your constructor depends on three parameters, This Is My recommended implementation of this code.

Class A () {public $ str = 'this is the properties'; public $ str2 = 'properties'; private $ str3 = 'private properties'; private static $ pool = []; public static function getInstance ($ a, $ B, $ c) {/*** assume that the object must be unique only when the three parameters are required * and the three parameters can be converted to the string type * (that is, they cannot be stream, array, or an object without _ toString () */$ key = implode ('_', func_get_args (); if (! Isset (self: $ pool [$ key]) {/*** for more information about the use of static keywords here, see the post-static binding * http://php.net/manual/zh/language.oop5.late-static-bindings.php * If Your PHP is still using a version earlier than 5.3, use * self :: $ pool [$ key] = new self ($ a, $ B, $ c); */self: $ pool [$ key] = new static ($, $ B, $ c);} return self: $ pool [$ key];} protected function _ construct ($ a, $ B, $ c) {// constructor. If private is not used, consider that if subclass inheritance exists.} public function func1 () {// class method} public function func2 () {// class method }}

PS In addition, it is recommended that the subject learn about the PSR specifications and autoload

It will report an error, and this writing method is very bad.

Not actually. The reason is as follows:
1. In a multi-threaded environment, if multiple threads enter at the same time, will multiple objects be generated at the same time? So you need to perform redundancy. I don't know how php works, but Java can use the sync keyword to implement synchronization.
2. Your constructor is public. Suppose someone else develops it twice in your code, but he doesn't know your getobj. He will create a new one. This will also cause problems.

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.