Instead of declaring each function as “static” in a class, is there any way that I can make a class itself “static”?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’d say the best way to go is to prevent object instantiation through a private constructor and explicitly marking all methods as static. Although you have to be careful to mark all methods as static (which is the result of static classes not existing in PHP), the benefit of this method over the Singleton approach is that static methods are more efficient than their non-static counterparts. You probably also want your class marked as final, as most static classes are not designed to be extended anyway (and it is good practice to do so).
An example would be something like this:
Furthermore, the Singleton pattern is now considered a bad practice by some.