Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP static properties and methods

Reference for PHP static properties and methods: declaring and calling with ::, self vs static, static return type, static variables and common mistakes.

A static property or method belongs to the class instead of to any one object. You reach it with the scope resolution operator ::, without creating an instance. All code shares one copy of a static property.

The keyword static has three other jobs: static variables inside functions, the return type static, and static:: for late static binding, covered below.

PHP
class Registry
{
    public static int $count = 0;

    public static function bump(): int
    {
        return ++self::$count;
    }
}
Registry::bump();
echo Registry::$count;   // 1

Try it

PHP Shared state and static methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP self:: versus static:: and static variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class C
{
    public static $prop = 1;
    public static function method(): static { return new static; }
}
C::$prop;           // read a static property
C::method();        // call a static method
$obj::$prop;        // also works through an instance's class
self::$prop;        // class where the code is written
static::$prop;      // class that was called (late static binding)
parent::method();   // the parent class
Form Meaning
self:: Resolves to the class in which the code was written.
static:: Resolves to the class the call was made on (see late static binding).
: static Return type meaning "an instance of the called class".
static $x = ...; Function-level variable that persists between calls.

Errors: a static method cannot use $this (throws Error). Calling a non-static method statically throws Error in PHP 8. Reading an undeclared static property throws Error.

Usage notes

  • Static properties are inherited but shared. A child class sees the parent's static property unless it redeclares it; redeclaring creates a separate copy.
  • Static state is global state. It makes code harder to test; prefer injecting objects, and keep statics for counters, caches and named constructors.
  • Named constructors (Money::fromCents()) are a clean use of static methods.
  • Typed static properties work like others and can be uninitialized.
  • Static variables in methods are shared by all instances. Since PHP 8.1, a method inherited without being overridden shares its static variables with the parent class.
  • Closures can be declared static function () {} so they do not capture $this, avoiding accidental memory retention.
  • Testing: static methods that call other static methods are hard to mock.