Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Static properties and methods

Regular properties and methods belong to an object: every object has its own data. Static ones belong to the class itself. They exist in a single copy and need no object.

A static method

A method with the word static is called through the class name and two colons:

<?php
 
class Math
{
    public static function square(int $number): int
    {
        return $number * $number;
    }
}
 
echo Math::square(5); // 25

No new Math() object was needed. Inside a static method there is no $this — there is no object, after all.

A static property

A static property is shared by every object of the class:

<?php
 
class Counter
{
    public static int $count = 0;
}
 
Counter::$count++;
Counter::$count++;
 
echo Counter::$count; // 2

The $ sign stays before the property name.

self

Inside the class, static members are reached through self:

<?php
 
class User
{
    private static int $created = 0;
 
    public function __construct(public string $name)
    {
        self::$created++;
    }
 
    public static function total(): int
    {
        return self::$created;
    }
}
 
new User('Anna');
new User('Oleg');
 
echo User::total(); // 2

There is one counter for all: every new object increases the shared value.

A factory method

A common use of static methods is creating an object under a clear name instead of the constructor:

<?php
 
class Color
{
    private function __construct(public int $red, public int $green, public int $blue) {}
 
    public static function fromHex(string $hex): self
    {
        return new self(...sscanf($hex, '#%02x%02x%02x'));
    }
}
 
$color = Color::fromHex('#7c3aed');
 
echo $color->red, ' ', $color->green, ' ', $color->blue; // 124 58 237

The constructor is closed with private, so an object can be created only through fromHex().

static instead of self

self always means the class the code is written in. static means the class the method was called on. The difference shows with inheritance:

<?php
 
class Model
{
    public static function create(): static
    {
        return new static();
    }
}
 
class Post extends Model {}
 
echo get_class(Post::create()); // Post

With new self() the result would be a Model object.

Class constants

Constants belong to the class too and are accessed with two colons:

<?php
 
class Order
{
    public const STATUS_NEW = 'new';
    public const STATUS_PAID = 'paid';
}
 
echo Order::STATUS_PAID; // paid

Common mistake

A static property is a variable shared by the whole program. Any code can change it, and finding out who did is hard. Do not keep data that belongs to a particular object in static properties:

<?php
 
class Cart
{
    public static array $items = [];
}
 
Cart::$items[] = 'tea';
Cart::$items[] = 'coffee';
 
echo count(Cart::$items); // 2 — one cart for every customer

Static methods are good for helper functions and factories; keep state in objects.

Thanks for reading!

Donate to the site — we work hard for you.