Class Constants and Static Members

5.Class Constants and Static Members

M

In this chapter

We'll learn class constants, static properties and static methods — members that belong to the class instead of to one object — how self::, parent:: and static:: point to them, why a static property shared by every booking mixed up two customers, and the static features of other languages that PHP doesn't have: static classes and static initializer blocks.

15–17 min

The Problem in Real Life

To avoid passing the customer around, Mia adds public static string $currentCustomer = ""; to Booking, sets it at the counter, and lets every new booking read it.

Two walk-ins arrive at once. Tom starts a booking for Sam Ortiz, then one for Ann Lee — and the screen now says Sam's booking is for Ann Lee too. Both bookings show the same customer, because there was only ever one $currentCustomer.

M

Each booking is its own object. How can two of them share one customer?

Mia

Belonging to an Object vs. Belonging to the Class

One value for everyone

A static property belongs to the class, so all objects share the same one — change it and every booking sees the change.

Fixed values with a name

Opening hours and the maximum bookings per day never change. Class constants give them a home inside the class.

self, parent, static

Three words that point at a class from inside it — and the scope resolution operator :: that uses them.

Class Constants and Static Members

Members of the class itself: everything so far belonged to one object: each booking has its own status. Some things belong to the class instead — the same for every booking:

  • Class constants: fixed values like Booking::MAX_PER_DAY.
  • Static properties and methods: variables and functions of the class, not of an object.
  • self, parent and static: pointing at a class from inside it.
  • Static features PHP doesn't have: static classes and static initializer blocks.

Class constants: const MAX_PER_DAY = 16; inside a class declares a constant that belongs to the class. Outside, you read it with the class name and ::, the scope resolution operator (Act 10 named it): Booking::MAX_PER_DAY. Inside the class, self::MAX_PER_DAY. Class constants can have a visibility (private const), since PHP 8.3 a type (const int MAX_PER_DAY = 16;), and since PHP 8.1 they can be final, so a child class can't change them. They're the right home for values that belong to an idea: the opening hour belongs to the shop, the maximum length of a note to a booking.

Class constants
<?php
declare(strict_types=1);
class Booking
{
public const int MAX_PER_DAY = 16; // typed class constant (PHP 8.3)
final public const string CURRENCY = "GBP"; // can't be overridden (PHP 8.1)
private const int MAX_NOTE_LENGTH = 200; // only for this class
public static function noteFits(string $note): bool
{
return mb_strlen($note) <= self::MAX_NOTE_LENGTH;
}
}
echo Booking::MAX_PER_DAY, " ", Booking::CURRENCY, "\n"; // 16 GBP
var_dump(Booking::noteFits("Likes a short fade")); // bool(true)

Static properties: public static string $currentCustomer belongs to the class, not to an object: there's exactly one of it, shared by every Booking, read as Booking::$currentCustomer (or self::$currentCustomer inside). That's Mia's bug. She treated it like a property of each booking, but setting it for Ann changed it for everyone, including Sam's booking, which only ever read the shared value. A static property is a global variable with a class name in front (Act 10), with all the same problems. Like every variable, it also lives only until the end of the request. The fix is to give each booking its own customer through the constructor (chapter 3). Use static properties rarely: for a cache of something expensive (Act 09's static in functions), not for data that differs per object. The diagram below shows the difference between a static property and normal ones.

One static value for the class, separate values per object

class Booking

static $currentCustomer = "Ann Lee" — ONE value, stored with the class

every object reads the same static value

$sam

own $status · reads Booking::$currentCustomer

$ann

own $status · reads Booking::$currentCustomer

result

Both bookings show Ann Lee

change the static once, every object sees it

Mia's bug: one static property shared by every booking
<?php
class Booking
{
public static string $currentCustomer = "";
public function customer(): string
{
return self::$currentCustomer; // reads the ONE shared value
}
}
Booking::$currentCustomer = "Sam Ortiz";
$sam = new Booking();
Booking::$currentCustomer = "Ann Lee";
$ann = new Booking();
echo $sam->customer(), " / ", $ann->customer(), "\n"; // Ann Lee / Ann Lee

Static methods: a method declared static belongs to the class and is called without an object: Booking::walkIn("Tom Hart"), like the named constructors in chapter 3. Because there's no object, there's no $this inside a static method. Static methods are right for creating objects (named constructors) and for small helpers that need no object at all. If a static method needs data, pass it in. If you find yourself writing many static methods that share static properties, you've rebuilt procedural code with a class name around it.

self, parent and static: three words point at classes from inside a class, always followed by ::. self means "the class this code is written in". parent means "the class this one inherits from" (chapter 7), as in parent::__construct(). static means "the class the method was actually called on", which only differs from self once there are child classes: new static(...) in Booking::walkIn() creates a GroupBooking when called as GroupBooking::walkIn(). That's called late static binding, and Act 12 covers it in depth. self::class, static::class and the magic constants __CLASS__ and __METHOD__ (Act 03) give the names as strings. Now you can read every line of the class Act 04 showed you — the code below repeats it.

self, parent and static — the class from Act 04, explained
<?php
declare(strict_types=1);
class Booking
{
public function __construct(public string $customer) {}
public static function create(string $customer): static // returns the class it was called on
{
return new static($customer);
}
public function describe(): string
{
return static::class . " for " . $this->customer . " (written in " . self::class . ")";
}
}
class GroupBooking extends Booking // chapter 7
{
public function describe(): string
{
return parent::describe() . ", group"; // run the parent's version first
}
}
echo Booking::create("Tom Hart")->describe(), "\n";
echo GroupBooking::create("Clare Lefèvre")->describe(), "\n";
// Booking for Tom Hart (written in Booking)
// GroupBooking for Clare Lefèvre (written in Booking), group

Static classes — not in PHP: C# has static classes: a class marked static can't be instantiated and may only contain static members, like C#'s Math. PHP doesn't support that keyword. The PHP equivalent is a class whose constructor is private — so new fails outside — and which is final (chapter 7) — so nobody can inherit from it. But in PHP, a group of helper functions often doesn't need a class at all: plain functions in a namespace (Act 13) do the same job.

C#: a static class (not in PHP) — and PHP's equivalent
// C# — a static class can't be instantiated and holds only static members
public static class Money
{
public static string Format(int pence) => $"£{pence / 100.0:0.00}";
}
// PHP has no "static class". The closest equivalent:
// final class Money
// {
// private function __construct() {} // new Money() fails outside
// public static function format(int $pence): string { ... }
// }
// — or, often simpler, a plain function formatPrice() in a namespace (Act 13).

Static initializer blocks — not in PHP: Java (static { ... }) and C# (a static constructor) let a class run code once, automatically, before it's first used, to set up its static properties. PHP doesn't support that either: static properties can only have constant default values. When a static value needs computing, use a static method that computes it on first call and keeps it in a static property — "lazy initialisation", as in Act 09's bank-holiday cache. The table below sums up static members.

Table — Instance members vs. static members
MemberBelongs toAccessed asHas $this?
PropertyEach object$booking->status—
MethodEach object$booking->confirm()Yes
Class constantThe classBooking::MAX_PER_DAY / self::MAX_PER_DAY—
Static propertyThe class: one shared valueBooking::$count / self::$count—
Static methodThe classBooking::walkIn() / static::walkIn()No
Static class (C#)—Not in PHP: final class + private constructor—
Static initializer (Java, C#)—Not in PHP: lazy static method—
Lazy initialisation instead of a static initializer block
<?php
declare(strict_types=1);
final class Holidays
{
private static ?array $dates = null;
private function __construct() {} // a static-only class
public static function all(): array
{
if (self::$dates === null) { // computed once, on first use
echo "(loading holidays)\n";
self::$dates = ["2026-12-25", "2026-12-28"];
}
return self::$dates;
}
}
echo count(Holidays::all()), "\n";
echo count(Holidays::all()), "\n";
// (loading holidays)
// 2
// 2

Key Takeaway

Class constants (Booking::MAX_PER_DAY), static properties and static methods belong to the class, not to one object, and are reached with :: — self:: inside the class, parent:: for the parent, static:: for the class actually called. A static property is one shared value, like a global variable: never use it for per-object data. Static methods have no $this and suit named constructors and helpers. PHP has no static classes (use final + private constructor, or plain namespaced functions) and no static initializer blocks (use lazy initialisation).

Why This Matters

Knowing what belongs to the class and what belongs to each object is what keeps two customers' bookings apart. A static property looks harmless, but it's shared state — the same trap as global variables, now with a class name in front.

Class constants and named constructors, on the other hand, are everywhere in good PHP: Booking::MAX_PER_DAY says where a number comes from, and Booking::walkIn() says how an object was made. And self, parent and static let you read any framework's source code.

Each booking gets its customer through the constructor again, and the static property is gone. Next, Olivia finds a confirmed booking whose price was changed after confirmation: the checkout said £27.50, the invoice now says £25.00, because a script lowered the price on the object. A confirmed price should never change. That's the next chapter: readonly properties and classes.