Object-oriented PHP
Modern PHP applications, including every Laravel and Symfony app, are built from classes. PHP 8 made classes much more pleasant: constructor promotion, readonly properties, enums, typed properties and attributes remove most of the boilerplate. This lesson covers the features you'll meet in real code.
Classes and objects
new Product(…)creates an object and calls__construct.$thisis the current object;->reads properties and calls methods.- Visibility:
public(anyone),protected(this class and subclasses),private(this class only).
Objects are handles
Variables hold a handle to an object, so passing an object to a function or assigning it shares the same object. Use clone for a copy:
Readonly properties and immutable objects
A readonly property can be set once (in the constructor) and never changed. Immutable value objects like Money are safe to share. __toString lets an object be used as a string.
Inheritance, abstract classes and interfaces
| Keyword | Means |
|---|---|
interface |
A list of methods a class promises to have; a class can implement many |
abstract class |
A partial class that can't be created on its own; subclasses fill in the abstract methods |
extends |
Inherit from one parent class |
parent:: |
Call the parent's version of a method |
final |
Can't be extended (class) or overridden (method) |
Code against interfaces (HasArea, a PaymentGateway, a Mailer), so implementations can be swapped, for example a fake mailer in tests.
Static members and constants
:: accesses class-level (static) members and constants. Use statics sparingly: shared state makes code harder to test.
Enums
Enums (PHP 8.1) define a fixed set of values, much safer than loose strings:
Status::from() throws on an unknown value; tryFrom() returns null. An enum can have methods and implement interfaces, and match over it is checked for missing cases.
Traits
A trait is a set of methods you can mix into several unrelated classes:
Namespaces and autoloading, briefly
Real projects put each class in its own file, inside a namespace that mirrors the folder structure:
<?php
// src/Shop/Cart.php
namespace App\Shop;
use App\Billing\Money; // import a class from another namespace
final class Cart { /* … */ }Composer's autoloader then loads App\Shop\Cart from src/Shop/Cart.php the first time it's used, so you never write require for your classes. That's covered in Composer, autoloading and testing.
Practice
- Write a
BankAccountclass withdeposit,withdraw(refusing overdrafts) and a private balance. - Create an interface
Notifierwithsend(string $to, string $msg), and two classes implementing it (email and SMS) that just print. - Make an enum
Size: string(S, M, L, XL) with a methodprice(): int. - Turn
Moneyinto a class withmultiply(int $times)and use it to total a cart.
1.What does constructor promotion (public function __construct(public string $name)) do?
2.What happens when you assign $b = $a where $a is an object?
3.How many interfaces can a class implement?
4.What does Status::tryFrom('lost') return for an unknown value?