Class-Related Types: static, self, parent, object and iterable

6.Class-Related Types: static, self, parent, object and iterable

M

In this chapter

We'll take a first look at the types that come with classes — a class name as a type, self, static, parent and object — and at array and iterable, so you can read any modern PHP signature now, long before you write your own classes in Act 11.

13–15 min

The Problem in Real Life

Marcus opens Booking.php, written by a contractor last year. Mia recognises the strict-types line and the int and string types. Then the rest: function create(string $customer): static. function rename(string $name): self. function base(): parent. function total(iterable $prices): int.

"These are all types," says Marcus. "They point at classes instead of at values like ints. You'll build classes yourself in Act 11 — today, just learn to read the words."

M

self, static, parent… these don't look like types at all. What kind of value is a "static"?

Mia

Types of Values vs. Types That Name Classes

Objects have types too

Every object belongs to a class — and that class name can be used as a type.

Families of classes

Classes can build on other classes, and types like parent and static follow that family tree.

Arrays and things like arrays

Some functions only need something they can loop over, array or not.

Class-Related Types: static, self, parent, object and iterable

Classes in one paragraph: a class is a blueprint for objects (chapter 3). A Booking class says what every booking has — a customer, a slot — and what it can do — its methods, which are functions that belong to the class. One class can extend another: a VipBooking class can build on Booking, getting everything Booking has and adding more. Booking is then its parent class. That's all you need for the types in this chapter:

  • A class name: Booking as a type means "an object of the Booking class, or of a class that extends it".
  • self: "an object of the class this code is written in".
  • static: "an object of the class this method was actually called on".
  • parent: "an object of the parent class".
  • object: "any object at all".
  • array and iterable: "an array", and "anything that can be looped over".

A class name as a type: you can use any class name as a type: function confirm(Booking $booking): void. PHP then accepts any Booking object — and any object of a class that extends Booking, like a VipBooking, because a VIP booking is a booking. That's the most common kind of type in object-oriented PHP. The instanceof operator asks the same question in code: $v instanceof Booking is true for a VipBooking.

self, static and parent: these three are used inside a class, so the code doesn't have to repeat the class name. self means "this class" — the one the code is written in. parent means "the class this one extends". static is subtle: it means "the class the method was actually called on". In the code below, create() is written in Booking with return type static, but called as VipBooking::create() — so it returns a VipBooking. With self it would only promise a Booking. Act 12 explains this properly (it's called late static binding). For now: self = this class, static = the class you called it on, parent = the class above.

Reading a small class with self, static and parent (don't worry about every detail)
<?php
declare(strict_types=1);
class Booking
{
public function __construct(public string $customer) {}
public static function create(string $customer): static
{
return new static($customer); // the class it was called on
}
public function rename(string $customer): self
{
$this->customer = $customer;
return $this; // this same object
}
}
class VipBooking extends Booking // VipBooking builds on Booking
{
public function base(): parent
{
return new parent($this->customer); // a plain Booking
}
}
$vip = VipBooking::create("Zoë");
echo get_class($vip), "\n"; // VipBooking: static followed the call
echo get_class($vip->base()), "\n"; // Booking
var_dump($vip instanceof Booking); // bool(true): a VIP booking is a booking

get_class() gives the name of an object's class. Act 11 explains every line of this.

object: object accepts any object of any class — but no strings, ints or arrays. Like mixed, it says very little, so a class name is usually better. It's useful for general helpers, like a function that logs the class of whatever object it's given.

object accepts any object — and only objects
<?php
declare(strict_types=1);
function className(object $thing): string
{
return get_class($thing);
}
echo className(new stdClass()), "\n"; // stdClass
echo className("text");
// TypeError: className(): Argument #1 ($thing) must be of type object, string given

array and iterable: array accepts any array (lists and named keys alike). iterable is wider: it accepts any array or any object that can be looped over with foreach (Act 06), like the ArrayIterator below. Use iterable when a function only needs to loop over the items, and doesn't care how they're stored. Since PHP 8.2, iterable is simply a short name for Traversable|array. The table sums up this chapter's types.

Table — Class-related types
TypeAcceptsYou'll use it in
Booking (a class name)A Booking, or an object of a class that extends itAct 11
selfAn object of the class the code is written inAct 11
staticAn object of the class the method was called on (return types)Act 12
parentAn object of the parent classAct 11
objectAny objectAct 11
arrayAny arrayAct 08
iterableAn array or a loopable object (Traversable|array)Acts 08–09
iterable: arrays and loopable objects
<?php
declare(strict_types=1);
function totalPence(iterable $pricesPence): int
{
$total = 0;
foreach ($pricesPence as $price) { // foreach: one item at a time (Act 06)
$total = $total + $price;
}
return $total;
}
echo totalPence([2500, 1000]), "\n"; // 3500: an array
echo totalPence(new ArrayIterator([2500, 1000])), "\n"; // 3500: a loopable object

Key Takeaway

With classes come more types: a class name (that class or any class extending it), self (the class the code is written in), static (the class the method was called on), parent (the parent class), object (any object), array, and iterable (an array or any object foreach can loop over). instanceof checks the same relationship in code.

Why This Matters

Almost every PHP library and framework — including Laravel, which BookBay will use after this course — is written with classes, and their function signatures are full of these types. Being able to read : static or iterable $items now means Mia can understand documentation and error messages long before she writes her first class.

It also plants an important idea early: a type can be a promise about behaviour ("can be looped over", "is a kind of Booking"), not only a kind of value. That idea is the heart of object-oriented PHP in Acts 11 and 12.

Mia can read every type in the codebase now. So Marcus brings her back to the partner bug that started this Act. "You know all the types. Now let's see exactly what PHP does when one type meets another — why "false" became true, why "25 USD" became 25 — and how to convert values on purpose instead of by accident." That's the next chapter.