Today I tried to write a singleton pattern, and then instantiated the singleton object twice and var_dumped it out respectively. Now the first result is null, and the second one shows the object type. What is going on? Thank you, teachers. Take a look, thank you!
You should put the returned singleton object outside the if judgment, otherwise the object cannot be returned when the object is not created for the first time
static public function GetConnec()
The first call does not return the new object, so it is null. The second call to singleton. Since an object has been instantiated the first time, the object instantiated for the first time is returned, and the value is returned for the second time. , the return value is the object instantiated by the first call