Open In App

When to use static vs instantiated classes in PHP?

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Static Function PHP

In PHP, we can have both static as well as non-static (instantiated) classes.

Static class

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 1:




<?php
class Class_name {
      
    // Static variable and static function
    // using static keyword
    public static $var = "text";
      
    public static function func() {
        echo self::$var;
    }
}
  
Class_name::func();
?>


Output:

text

Example 2: This example checks if a string created has a length greater than or equal to 7 or not.




<?php
  
class GFG
{
    // Static variable
    public static $num1 = 7;
  
    // Static function
    public static function check($var)
    {
        // Accessing static variable using
        // self keyword
        if(strlen($var) >= self::$num1)
            return true;
        else
            return false;
    }
}
  
// String is created
$str = "GeeksforGeeks";
  
// Static function is called 
// using scope resolution operator
if(GFG::check($str))
    echo "String is valid!";
else
    echo "String is NOT valid!";
      
?>


Output:

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 1:




<?php
class GFG {
      
    // non-static variable
    // and function
    public $var = "text";
    public function func()
    {
        echo $this->var;
    }
}
  
$test = new GFG();
$test->func();
  
?>


Output:

text

Example 2: This program checks if a created string length greater than or equal to 7 or not.




<?php
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!";
      
?>


Output:

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.



    Last Updated : 30 Apr, 2019
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads