I am currently a beginner in PHP OOP programming and was wondering if someone could provide an example of how using static properties and methods are useful in real web applications.
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.
Although statics can have problems as pointed out in the comments, they do have a few uses.
One which I’ve found quite useful is “named constructors”.
Typically a class would have a single constructor. But what if the class could be initialized in several different ways?
For example, perhaps your application supports creating users with an email address, or from a Facebook User ID…
As you can see above, it could be a bit confusing to what the parameter in the constructor was when you use the class.
Instead, by writing it like this…
…and it suddenly makes much more sense.