What are the use of static functions in OOP?
How it is differentiated from other function ?
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.
As a very simple, somewhat contrived example:
In this case the static function serves as an alternative constructor, allowing you to construct the object from an array of data instead of separate arguments.
Generally, static functions provide functionality around an object without needing to instantiate it. There are many uses for that. At the very least, entirely static classes which are never instantiated can be used to bundle functions and related data together, which in itself makes code cleaner. That’s where they differ from normal functions: normal functions cannot save “external” data (without using globals, which you don’t want to do), static class methods can save data in static class properties.