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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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();
?>

chevron_right


Output:



text

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

text

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

String is valid!
String is NOT valid!

Static Class vs Instantiated Class