Static scope
The word static has three jobs in PHP: it keeps a local variable alive between calls, it marks a method that needs no object, and it means the class of the caller in late static binding. The blocks below show one job each.
Static locals
A static local keeps its value between calls while staying hidden inside the function. The first call sets it up and later calls reuse it.
Counters and memo caches are classic uses that avoid global variables.
<?php function nextId(): int { static $n = 0; $n = $n + 1; return $n; } echo nextId(), PHP_EOL; echo nextId(), PHP_EOL; echo nextId(), PHP_EOL;
php staticvar.php
1
2
3
The initialiser runs once, so the three calls continue one sequence instead of restarting at zero.
Static methods
Static methods belong to the class itself and run without an object. Call them with two colons after the class name.
They suit pure helpers and factories that build objects from simple input.
<?php class Str { public static function shout(string $s): string { return strtoupper($s) . "!"; } } echo Str::shout("hi"), PHP_EOL;
php staticmethod.php
HI!
No object was built: the two colons reach the method through the class name, and the helper only touches its argument.
self vs static
Keyword self always points at the class where the code lives. Keyword static points at the class used in the actual call.
Child classes inherit factories, so static returns the child while self would freeze the parent.
<?php class Base { public static function make(): static { return new static(); } public static function makeSelf(): self { return new self(); } } class Child extends Base { } echo get_class(Child::make()), PHP_EOL; echo get_class(Child::makeSelf()), PHP_EOL;
php self.php
Child
Base
Both factories are called through Child: the one that returns static builds the child, and the one that returns self stays in the parent, which is the whole difference between the two keywords.
Singleton sketch
A singleton stores one shared instance in a static property. A static maker builds it once and returns the same object every time.
Use this shape sparingly for registries and configs, since global state makes tests harder.
<?php class Config { private static ?Config $one = null; public static function shared(): Config { if (self::$one === null) { self::$one = new Config(); } return self::$one; } } var_dump(Config::shared() === Config::shared());
php singleton.php
bool(true)
The second call finds the stored object instead of building another one, and the strict comparison proves that both calls returned the same instance.
Exercises
- Write a function with a static counter that returns the count so far and call it four times.
- Build a static factory that returns a named object and print its class.
- Add a self factory next to a static one and compare the classes of their results.
- Give the singleton a second method that resets the stored instance and prove the next call builds a new one.
- Explain why a static local is not the same thing as a global variable.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a
staticlocal variable keep between calls?Answer
Its value, while it stays hidden inside the function
The initialiser runs on the first call only, so the value survives in the function scope.
-
How is a static method called?
Answer
With two colons after the class name
The class name and the scope resolution operator reach the method without an instance.
-
What does
selfrefer to inside a method?Answer
The class where the code is written
self is fixed at the class that declares the method, which is why late static binding exists.
-
What does
staticrefer to inside a method?Answer
The class used in the call
Late static binding resolves static to the class of the actual call, child classes included.
-
What does this program print?
<?php function nextId(): int { static $n = 0; $n = $n + 1; return $n; } echo nextId(), nextId(), nextId();Answer
123
The static value is set once and each call adds one to the value left by the previous call.
-
What does this program print?
<?php class Str { public static function shout(string $s): string { return strtoupper($s) . "!"; } } echo Str::shout("hi");Answer
HI!
The static call needs no instance, and strtoupper() uppercases the argument before the mark is added.
-
What does this program print?
<?php class Base { public static function make(): static { return new static(); } } class Child extends Base { } echo get_class(Child::make());Answer
Child
The return type and the constructor both use static, so the call through Child builds a Child.
-
What does this program print?
<?php class Base { public static function makeSelf(): self { return new self(); } } class Child extends Base { } echo get_class(Child::makeSelf());Answer
Base
self is fixed in the declaring class, so the child call still builds a Base object.
-
What does this program print?
<?php class Config { private static ?Config $one = null; public static function shared(): Config { if (self::$one === null) { self::$one = new Config(); } return self::$one; } } var_dump(Config::shared() === Config::shared());Answer
bool(true)
The static property keeps the first object, so the second call returns the same instance.
-
Why does the lesson call the singleton shape a sparing tool?
Answer
It hides global state, which makes tests harder
One shared instance is global state reached from everywhere, and a test cannot replace it easily.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev