What is static keyword in PHP?

Overview

The static keyword is used to declare property or method of a class as static.

If we declare a property or method as static in a class, we can access them without instantiating the class itself. The property or method can also be accessed statically within an instantiated class object.

We can also define static variables, static anonymous functions by using static keyword.

A static variable like all PHP variable exists only in a local function scope but a static variable retains its value when program execution leaves this scope. A static variable is very useful in recursive functions where retaining data across calls is necessary.

In a static anonymous function the current class could not be automatically bound. We will use this when we do not need to reference the class within the function scope;

Syntax

To declare a property or method as static in a class we need to add the static keyword before the property or method. The syntax is as follows.

class SomeClass{
    // Declare static property
    public static $staticVar = '';

    // Declare static method
    public static function staticMethod()
    {
        //
    }
}
// Declare static variable
static $count = 0;
$func = static function() {
            //
};

Example of static property and method

We will understand the use of static keyword in the following example. In this example we will declare static properties and static methods and access them by two ways – instantiating the class and without instantiating the class. For this we will create a php file and write the following code in it.

<?php
class StaticClass {
    public static $staticVar = 'Static Variable';
    public static function staticMethod(){
        return 'Static Method';
    }
}

// Accessing without instantiating
echo StaticClass::staticMethod();
echo "\n";
echo StaticClass::$staticVar;
echo "\n";

// Accessing with instantiating
$obj = new StaticClass();
echo $obj::staticMethod();
echo "\n";
echo $obj::$staticVar;
?>

If we execute the above file in the browser we will get the following output:

Static Method
Static Variable
Static Method
Static Variable

Example of static variable

In the example below, we will create a function named sum and declare a static variable $count with an initial value of 0. After that I will increment this by1 and will return the value. Since it is a static variable, $count will not lose its value. If we call the sum function three times we will get the output: 123.

<?php
function sum() {
    static $count = 0;
    $count++;
    echo $count;
}

sum(); // Output: 1
sum(); // Output: 2
sum(); // Output: 3
?>

Example of static anonymous function

A static anonymous function cannot access $this. In the following example we will try to use $this inside a static anonymous function and a non-sttaic anonymous function.

<?php
class SomeClass
{
    public function someMethod()
    {
        // Defining a non-static anonymous function
        $nonStaticFunc = function() {
            var_dump($this);
        };
        $nonStaticFunc();
        // Output: object(SomeClass)#1 (0) {}
        
        // Defining a static anonymous function
        $staticFunc = static function() {
            var_dump($this);
        };
        $staticFunc();
        // Output: Fatal error: Uncaught Error: Using $this when not in object context
    }
}

$obj = new SomeClass();
$obj->someMethod();
?>

In the above example, I have created a class named SomeClass and created a method named someMethod in this class. I have created two anonymous functions within someMethod , a non-static and the other is static. Then I Attempted to access $this within these two anonymous functions. Then I called these two anonymous functions. Finally created an object of SomeClass and called someMethod method.

For the non-static anonymous function call I get the output: object(SomeClass)#1 (0) {} and in case of anonymous static function call I get the output: Fatal error: Uncaught Error: Using $this when not in object context in …

In this article I discussed static keyword in PHP. I Hope this will be useful for you.