Change language

When to use static and instantiated classes in PHP?

In PHP we can have both static and non-static (instantiated) classes. Static class Introduction:static class in PHP - it is a class type that is created only oncein a program. It must contain a static member (variable) or a static member function (method), or both. Variables and methods are accessed without creating an object using the scope resolution operator (: :). But there is one catch here, that a static method cannot access non-static variables, because that would require creating an object first. So, to access the variables of a static class, we must declare them as static using the statickeyword.Example 1: class Class_name { // Static variable and static function // using the static keyword public static $var = "text" ; public static function func() { echo self :: $var ; } }  
text  Example 2: This examplechecks if the generated string is greater than or equal to 7 in length or not.       class  GFG  {   // Static variable  public  static  $num1  = 7;   // Static function  public  static  function  check (  $var )   {   // Access static variable using  // self keyword  if  (  strlen  (  $var ) > = self ::  $num1 )   return  true ;   else  return  false;  } }  
// Row created $str = "GeeksforGeeks" ;  
// The static function is called
// using the scope resolution operator if (GFG::check ( $str )) echo "String is valid!" ; else echo " String is NOT valid! " ;  
String is valid !
Classified (non-static) class Introduction:instantiation means instantiation object in server memory. The generated classes - these are the classes that require the creation of an object before calling its variables and methods. It is similar to a regular class used in C++, Java, and other programming languages. These classes can be created more than onceand contain unique values ​​ for each of their objects.Example 1: class GFG { // non-static variable // and function public $var = "text" ; public function func() { echo $this -> var ; } } $test = new GFG(); $test -> func();  
text  Example 2:This program checks if the length of the generated string is greater than or equal to 7 or not.       class  GFG {   // Non-static variable   public  $num1  = 7;   // Non-static function  public  function  check (  $var )   {   if  (  strlen  (  $var ) > =  $this  -> num1)   return  true;   else  return  false;  } }  
// Object 1 is created $str1 = new GFG(); if ( $str1 -> check ( "GeeksforGeeks" )) echo "String is valid!" ; else echo " String is NOT valid! " ;  
// Object 2 is created

$str2 = new GFG(); if ( $str2 -> check ( "Geeks" )) echo "String is valid!" ; else echo " String is NOT valid! " ;  

String is valid ! String is NOT valid!
Static Class versus Created Class
  • A static class is used for one instance of that class, whereas a class instance is used when more than one instance is required.
  • A static class contains static variables and static methods, while an instance of a class contains non-static variables and non-static methods.
  • Programs with static classes are difficult to test and extend, while programs with non-static classes allow easy testing and extension of properties.
  • Data is mostly associated with the class itself in the case of a static class, whereas in the case of an instance of a class, the data is associated with the corresponding object rather than the class itself. When to use what?
    Consider using a static classif any of these statements apply:
    • Functionality of methods and variables x classes are shared (global) .
    • When you want the corresponding field or variable to have the same value cross-instance, or when you want a separate instance of that class.
    • When creating a singleton(a special kind of class that can only be created once) or a helper(helper) class.
    • When each object has the same data and requires the creation of a class that only works with this data, then a static class can be used.
    • Testing(mostly single) or maintainability are not required.
    Consider using the Instantiated class if any of these statements apply to your situation:
    • The functionality of the class is not is global,or there can be multiple instances of this class.
    • When each object of the class has its own o and unique data (such as email ID).
    • Testing and maintainability required.
    Example:Consider a case where the store has invoices generated for purchases that contain the name of the item, the value of each item, total value, date, store name, registered store number, address, etc. Here, for different customers purchasing different For items, the value of item name, total cost, date etc. will be different. Thus, they can be obtained using an instance of a class, where each client is represented as an objectof that class, and each object will have different attributes. But the name of the store, the registered store number, the price of the corresponding item and the address will be the same for all buyers. So here the staticclass can be used to instantiate all of these values ​​once,and then they can be used many times.


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method