Classes that use the Singleton mode have some questions about their static members.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.