When to use static vs instantiated classes in PHP?
Prerequisite – Static Function PHP
In PHP, we can have both static as well as non-static (instantiated) classes.
Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::). But here is a catch, that the static method cannot access the non-static variables because that will require the creation of the object first. So, to access variables of a static class we must declare them as static using keyword static.
Example 2: This example checks if a string created has a length greater than or equal to 7 or not.
String is valid!
Instantiated (Non-Static) Class
Introduction: Instantiating means to create an instance of an object in server’s memory. Instantiated classes are those classes which require an object to be created before it’s variables and methods are called. It is similar to a normal class used in C++, Java and other programming languages. These classes can be instantiated more than once and holds unique values for each of its object.
Example 2: This program checks if a created string length greater than or equal to 7 or not.
String is valid! String is NOT valid!
Static Class vs Instantiated Class
- Static class is used for a single instance of that class whereas instantiated class is used when more than one instance is required.
- Static class contains static variables and static methods whereas instantiated class contains non-static variables and non-static methods.
- Programs having static classes are hard to test and to extend while programs with non-static classes provide easy testing and extending property.
- Data is mainly related to the class itself in case of static class while in case of an instantiated class, data is related to the respective object, not the class itself.
When to use what?
Consider using Static class if any of these statements apply to situation:
- Functionality of the methods and variables of a class are general (global).
- When you want a respective field or variable to have same value cross-instance or when you want single instance of that class.
- When creating a singleton (particular kind of class that can be instantiated only once.) or a utility (helper) class.
- When each object is having same data and require to create a class that just works only on this data, then static class can be used.
- Testing (mainly unit) or maintainability is not required.
Consider using Instantiated class if any of these statements apply to situation:
- Functionality of the class is not global or there might be more than one instance of that class.
- When each object of the class has its own unique data (like email ID).
- Testing and maintainability is required.
Example: Consider in case of a shop, the bills generated for purchase items contains the name of items, cost of each item, total cost, date, shop name, registered shop no., address, etc. Here, for different customers purchasing different items the value of the item’s name, total cost, date, etc will be different. So these can be obtained using instantiated class where each customer is represented as an object of that class and each object will have different attributes. But the name of the shop, registered shop no., cost of respective item and address will be the same for all the customers. So here static class can be used to instantiate all these values one time and then these can be used many times.My Personal Notes arrow_drop_up