A class with a singleton pattern, with some questions about its static members
This post was last edited by Fox_lin on 2015-11-16 17:08:20
The database class for the following singleton pattern:
Class database{
static private $db;
Private 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.
------to solve the idea----------------------
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.