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.
- 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.
- 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.
- How to create static classes in PHP ?
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Understanding "static" in "public static void main" in Java
- Use of :even and :odd pseudo-classes with list items in CSS
- New self vs. new static in PHP
- Comparison between static and instance method in PHP
- Static Function in PHP
- What is Late Static Bindings in PHP ?
- Why does PHP 5.2+ disallow abstract static class methods ?
- Difference between self::$bar and static::$bar in PHP
- PHP | Classes
- Abstract Classes in PHP
- PHP | Predefined classes and Interfaces
- Difference between Static and Shared libraries
- “static const” vs “#define” vs “enum”
- Build a Simple static file web server in Node
- Static vs Dynamic Website
- Ruby Static Members
- How to remove .html extension from URL of a static page ?
When to use what?
Consider using Static class if any of these statements apply to situation:
Consider using Instantiated class if any of these statements apply to situation:
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.