In PHP, the "Singleton mode" instance description assumes that we need to write a class to operate the database and meet the following requirements:
① The SqlHelper class can have only one instance (no more)
② The SqlHelper class must be able to create this instance by itself
③ You must provide this instance to the entire system. In other words, multiple objects share A memory area. for example, if object A has set certain attribute values, object B, C can also access these attribute values (this problem is well illustrated in the example at the end)
1
_ Dbname; 10} 11 public function setDbName ($ dbname) {12 $ this-> _ dbname = $ dbname; 13} 14 public function clear () {15 unset ($ this-> _ dbname); 16} 17 18} 19 $ sqlHelper = new SqlHelper (); // Print: Fatal error: Call to private SqlHelper :: __construct () from invalid context 20?>
The above SqlHelper class cannot create instances from outside its own class, because we set the constructor to private, so we use new SqlHelper () private constructors cannot be used outside the class. if forced, the following error will be reported:
Fatal error: Call to private SqlHelper :__ construct () from invalid context
Severe error: A private constructor SqlHelper :__ construct () is called from the context ()
According to the previous thinking logic, instantiating a class directly uses the new operator outside the class, but since the constructor is set to private, we know that, private member attributes or functions can only be accessed within the class, so we can create another function (such as getInstance () within the class SqlHelper, and must be public, in the getInstance () function, the SqlHelper class is instantiated.
For example:
1
_ Instance = new SqlHelper (); 7} 8 // ...... omitted 9} 10?>
But the problem arises,
① We didn't instantiate the SqlHelper object before calling getInstance (), so we couldn't call the getInstance () function through the object,
② Since the object has not been converted into an instance when getInstance is called, Using $ this in the getInstance function will certainly report an error (Fatal error: Using $ this when not in object context)
How can this problem be solved?
Solution: we can say that the getInstance () method is set to static. according to the static definition, it can only be called by classes rather than objects and set $ _ instance to static. So this method exactly fits our taste.
Therefore, we further modify the code as follows:
1
By using the getInstance function, the current memory contains errors when an instance of the class is judged. if not, the instance is instantiated and the object handle is returned. If yes, the object handle is directly returned.
The complete code is as follows:
1
_ Dbname; 19} 20 public function setDbName ($ dbname) {21 $ this-> _ dbname = $ dbname; 22} 23} 24 // $ sqlHelper = new SqlHelper (); // Print: Fatal error: Call to private SqlHelper :__ construct () from invalid context 25 $ A = SqlHelper: getInstance (); 26 $ A-> setDbName ('database name'); 27 $ A-> getDbName (); 28 // unset ($ ); // Remove reference 29 $ B = SqlHelper: getInstance (); 30 $ B-> getDbName (); 31 $ C = SqlHelper: getInstance (); 32 $ C-> getDbName (); 33 34? >
The execution result of the above code:
Database name // $ A-> getDbName ();
Database name // $ B-> getDbName ();
Database name // $ C-> getDbName ();
That is to say, objects A, B, and C all use the same object instance and access the same memory area.
Therefore, even if unset ($ A), objects B and C can still use the getDbName () method to output the "database name"
Unset ($ A) actually only disconnects the contact information between object A and A memory address (the address of the instance where the object is located), and has nothing to do with object B and object C, you can use a picture to represent the following: