The class that uses the Singleton mode has some questions about its static members. This post was last edited by fox_lin from 2015-11-1617: 08: 20, for example, the database class in the Singleton mode: class & nbsp; Database {static & nbsp; private & nbsp; $ db; private & nbsp; function & nbsp; _ class in Singleton mode is used, some questions about its static members
This post was last edited by fox_lin at 17:08:20
For example:
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;
}
}
}
There is a saying that static methods cannot call non-static members.
In this class definition, the self: $ db = new self (); isn't this self () method a non-static constructor?
Or is it inaccurate to say that "static methods cannot call non-static members? Please advise me.
------ Solution ----------------------
Both self and $ this refer to the class itself.
However, the former is used in static methods, and the latter is used in dynamic (instantiated object) methods.
New is an instantiation class, and the constructor cannot be static.
Note: Due to historical reasons, if the E_STRICT level check is disabled, php classes will not be static or non-static.