The database class for the following singleton pattern:
Class Database{static Private $db;p rivate function __construct () {}static function getinstance () {if (self:: $db) {return Self:: $db;} Else{self:: $db = new self (), return self:: $db;}}}
One argument is that a static method cannot invoke a non-static member.
So there is a sentence in the definition of this class: self:: $db = new self (); Is this self () method not a non-static constructor?
Is it inaccurate to say that static methods cannot invoke non-static members? Please advise your predecessors.
Reply to discussion (solution)
Well, it's wrong, that's the statement that instantiates the object, not the one that called the member.
Self and $this both refer to the class itself
Only the former is used in static methods, which are used in dynamic (instantiated objects) methods
New is an instantiated class, constructors cannot be static
Note that due to historical reasons, the PHP class will not be static or static, because it shuts down the e_strict level check.
Error_reporting (e_all ^ e_notice ^ e_strict); class T { function A () { echo ' abc '; }} T::a ();
Abc
This is what PHP is all about.
Did you get rid of those locks and make them happy?
Self and $this both refer to the class itself
Only the former is used in static methods, which are used in dynamic (instantiated objects) methods
New is an instantiated class, constructors cannot be static
Note that due to historical reasons, the PHP class will not be static or static, because it shuts down the e_strict level check.
Yes, I get it, thanks for the answer.