PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Static Variables in PHP

Static variables in PHP are an essential feature that allows you to define variables that retain their value across function calls and class instances. Understanding static variables in PHP requires exploring their behavior in both functions and classes.

1. Static Variables in Functions

Static variables in functions maintain their value between calls to the function. Unlike regular local variables, which are re-initialized every time the function is called, static variables are initialized only once and retain their value across multiple calls.

Example:

				
					function counter() {
    static $count = 0;
    $count++;
    echo $count;
}

counter(); // Outputs: 1
counter(); // Outputs: 2
counter(); // Outputs: 3

				
			

In this example, the’ $count‘ variable is declared as ‘static‘, so it is only initialized once. Each time the ‘counter‘ function is called, ‘$count‘ retains its previous value and increments by one.

2. Static Variables in Classes

Static variables (or properties) in classes are shared among all instances of the class. This means that static properties belong to the class itself rather than any specific instance. They can be accessed using the self keyword within the class or the class name itself outside the class.

Defining Static Properties:

				
					class Example {
    public static $staticVar = 'initial value';

    public static function staticMethod() {
        echo self::$staticVar;
    }
}

// Accessing static property
Example::$staticVar = 'new value';
Example::staticMethod(); // Outputs: new value

				
			

In this example, ‘staticVar‘ is a static property of the ‘Example‘ class. It can be accessed and modified using the class name ‘Example::staticVar‘.

3. Static Methods

Static methods are methods that belong to the class itself rather than any instance of the class. They can be called without creating an instance of the class and can access static properties directly.

Defining and Using Static Methods:

				
					class MathOperations {
    public static function add($a, $b) {
        return $a + $b;
    }
}

// Calling static method
$result = MathOperations::add(5, 3);
echo $result; // Outputs: 8

				
			

In this example, the’ add‘ method is static and can be called directly using the class name ‘MathOperations::add‘.

4. Static Constants

Constants in classes can also be defined as static. They are declared using the ‘const‘ keyword and are implicitly static.

Example:

				
					class Config {
    const DB_HOST = 'localhost';
    const DB_USER = 'root';
}

// Accessing static constant
echo Config::DB_HOST; // Outputs: localhost

				
			

In this example, ‘DB_HOST‘ is a static constant and can be accessed using the class name ‘Config::DB_HOST‘.

5. Scope Resolution Operator (::)

The scope resolution operator (‘::‘), also known as the Paamayim Nekudotayim or double colon, is used to access static properties and methods.

Example:

				
					class Example {
    public static $staticVar = 'value';

    public static function staticMethod() {
        return 'static method';
    }
}

echo Example::$staticVar; // Outputs: value
echo Example::staticMethod(); // Outputs: static method

				
			

Static variables in PHP provide a mechanism for retaining values across function calls and sharing data among class instances. Key points to remember include:

  • Static Variables in Functions: Retain their value between function calls.
  • Static Properties in Classes: Shared among all instances of the class.
  • Static Methods: Belong to the class and can be called without creating an instance.
  • Static Constants: Defined using the const keyword and are implicitly static.
  • Scope Resolution Operator (::): Used to access static properties and methods.

These features are crucial for efficient memory management and consistent data handling across different parts of a PHP application.

Scroll to Top