static
In PHP, the "static" keyword is used to define class-level properties and methods that can be accessed without instantiating an object of the class. In this
The PHP static Keyword
The static keyword in PHP has two distinct uses, and it helps to keep them separate from the start:
- Class members —
staticmarks a property or method as belonging to the class itself rather than to any individual object. You access it without creating an instance, and all instances share the same value. - Static local variables — inside a function,
staticmakes a local variable keep its value between calls instead of being reset each time.
This chapter covers both, plus the related keywords self, static::, and parent:: that you almost always meet alongside static members. If classes and objects are new to you, read Classes and Objects first.
Static class members
A normal property lives on each object. A static property lives on the class — there is exactly one copy, shared by every instance and accessible even when no instance exists.
Basic syntax
<?php
class MyClass {
public static $myProperty = "Hello, world!";
public static function myMethod() {
return self::$myProperty;
}
}You read a static member with the scope resolution operator ::, not the object operator ->:
<?php
echo MyClass::$myProperty; // Hello, world!
echo MyClass::myMethod(); // Hello, world!Inside the class, refer to your own static members with self:: (or static::, explained below) — never $this, because a static method may run with no object at all.
For deeper, focused coverage see Static Properties and Static Methods.
Static local variables
The same keyword does something completely different inside a function: it makes a variable persist across calls. Without static, a local variable is re-initialised on every call; with it, the initial value is set once and the variable keeps whatever it held last time.
<?php
function counter() {
static $count = 0; // initialised only on the first call
$count++;
return $count;
}
echo counter(); // 1
echo counter(); // 2
echo counter(); // 3This is handy for caching an expensive result or counting how many times a function ran, without leaking a global variable.
Examples
Let's look at some practical examples of static class members:
Examples of PHP static keyword
<?php
// Example 1
class Counter
{
public static $count = 0;
public static function increment()
{
self::$count++;
}
}
Counter::increment();
Counter::increment();
echo Counter::$count . PHP_EOL;
// Example 2
class User
{
public static $name;
public static function setName($name)
{
self::$name = $name;
}
}
User::setName("John Doe");
echo User::$name;In Counter, the static property $count is shared, so two calls to increment() accumulate to 2. In User, setName() stores data on the class itself rather than on any object.
self vs static (late static binding)
When you reference a static member from inside a method you have two options, and the difference matters in inheritance:
self::is resolved at compile time — it always points to the class where the code is written.static::uses late static binding — it resolves at runtime to the class that was actually called.
<?php
class Base {
public static function create() {
return new static(); // runtime class
}
public static function createSelf() {
return new self(); // always Base
}
}
class Child extends Base {}
echo get_class(Child::create()); // Child
echo "\n";
echo get_class(Child::createSelf()); // BaseUse static:: when a subclass should be able to override behaviour or be the one instantiated — the pattern above is the backbone of factory methods. Use self:: when you specifically mean this class. To call a parent's static method, use parent::. See Inheritance for the broader picture.
Common use cases
- Counters and shared state — a single value tracked across all instances, like the
Counterabove. - Utility / helper methods — stateless functions grouped under a class, e.g.
Math::clamp(), called without an object. - Factory methods —
User::fromArray($data)returns a configured instance;new static()keeps it subclass-friendly. - Singletons and simple caches — a static property holds the single instance or a memoised result.
For named, immutable shared values, prefer a class constant over a static property.
Gotchas
- No
$thisin static methods. A static method can be called without an object, so$thisis undefined there. Reaching for instance state from a static method is a design smell. - Static properties are global-ish shared state. Because every instance shares the one copy, mutating it from anywhere affects everyone — easy to introduce hidden coupling and to break test isolation. Reach for it deliberately.
::for static,->for instance.MyClass::$prop(static) vs$obj->prop(instance). Note$stays on the property name in the static form:Counter::$count, notCounter::count.- Static local variables are per-function, not per-call. Two recursive calls share the same
staticvariable, which is occasionally surprising.
Conclusion
The static keyword does two jobs: it defines class-level properties and methods shared across all instances and reachable without an object, and it makes a function's local variable survive between calls. Combine it with self::, static::, and parent:: to control exactly which class your static calls resolve to. Use static members for genuinely shared state and stateless helpers — but treat shared mutable static data with the same caution you would a global.