Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PHP
PHP
0/12 completed
Beginner
Hello PHPVariables and arraysControl flowFunctions
Intermediate
Strings, dates and JSONForms, requests and sessionsDatabases with PDO
Advanced
Object-oriented PHPErrors, exceptions and typesSecurity essentials
Pro
Composer, autoloading and testingLaravel basics
Tracks / PHP / Advanced

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

PHP · editable
  • new Product(…) creates an object and calls __construct.
  • $this is 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:

PHP · editable

Readonly properties and immutable objects

PHP · editable

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

PHP · editable
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

PHP · editable

:: 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:

PHP · editable

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:

PHP · editable

Namespaces and autoloading, briefly

Real projects put each class in its own file, inside a namespace that mirrors the folder structure:

php
<?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

  1. Write a BankAccount class with deposit, withdraw (refusing overdrafts) and a private balance.
  2. Create an interface Notifier with send(string $to, string $msg), and two classes implementing it (email and SMS) that just print.
  3. Make an enum Size: string (S, M, L, XL) with a method price(): int.
  4. Turn Money into a class with multiply(int $times) and use it to total a cart.
✎ Check your understanding

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?

← Databases with PDO