Static Variables in PHP

Subodh Poudel Jun 21, 2022
  1. Static Variables in PHP
  2. Use self Keyword to Access Static Variable Inside the Class in PHP
  3. Use Class Name to Access Static Variable Outside the Class in PHP
  4. Access the Static Variable Inside the Static Method
Static Variables in PHP

This article will introduce the PHP static variables, explain their characteristics and demonstrates various ways to access the static variables in an object-oriented context.

Static Variables in PHP

Variables in a program are used to store data or values that can be used later in a program. A variable can store characters, numeric values, strings, memory addresses, etc.

In PHP, we declare or store values using the $ dollar sign followed by the name of the variables.

Example:

$name = "John Doe";
$marks = 90;

Likewise, a static variable is a variable whose scope does not end outside the function. The scope of a non-static variable is destroyed after the function exits.

We can declare a static variable using the static keyword.

Example:

static $name = "John Doe";
static $marks = 90;`

Let’s see the property of the static variable with the following demonstration.

Code Example:

function total_marks () {
    static $marks = 90;
    $marks ++;
    print $marks;
    print "<br />";
}
total_marks();
total_marks();
total_marks();

Output:

91
92
93

The code above shows that the static variable preserves its value even after the function has ended. There are three different calls to the function total_marks(), but the value of $marks is not destroyed even after the function is exited.

The value does not reset to 90 when the second and the third function call is made. It keeps on incrementing as the previous value is preserved.

Let’s apply the above example to a non-static variable.

Example Code:

function total_marks () {
    $marks = 90;
    $marks ++;
    print $marks;
    print "<br />";
}
total_marks();
total_marks();
total_marks();

Output:

91
91
91

The value of the $marks variable resets to 90 every time the function call is made. It shows that the non-static variable does not preserve its previous value.

Use self Keyword to Access Static Variable Inside the Class in PHP

We can use the self keyword to access a static variable inside a class. A double-colon :: is used after the self keyword and the static variable follows right after it.

The this keyword does not work for static variables as the static variable does not belong to an object.

For example, in a class, Student create a static variable $name and assign a value John Doe. Inside a function, who() use the self keyword with the double-colon to access the static variable $name, and lastly, invoke the function with an object of the class.

Example Code:

class Student {
    public static $name = "John Doe";
    public function who() {
    echo self::$name;
    }
}
$obj = new Student;
$obj->who();

Output:

John Doe

Use Class Name to Access Static Variable Outside the Class in PHP

Static variables can be accessed directly without creating an instance of the class. To access a static variable outside the class, we can write the name of the class followed by a double-colon(::) followed by the name of the static variable.

For example, inside a class, Student create a static variable as in the first method. Next, write Student:$name outside the class to access the static variable.

Example Code:

class Student {
    public static $name = "John Doe";
}
echo Student::$name;

Output:

John Doe

The static variable $name is accessed outside the class using the class name.

Access the Static Variable Inside the Static Method

In the first example, we learned about accessing static variables inside a class. There we have accessed a static variable inside of a non-static method.

This section will place the static variable inside a static method and access it. We can write the static keyword before the function to create a static method, access static functions without creating an instance of the class, and use the class name to call the static method.

Example Code:

class Student {
    public static $name = "John Doe";
    public static function who() {
    echo self::$name;
    }
}
Student::who();

Output:

John Doe

Here, we have accessed the static method who() with the class Student using the :: operator. We used the self keyword to access the static variable inside the method.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn