Skip to content

Latest commit

 

History

History
62 lines (44 loc) · 3.96 KB

File metadata and controls

62 lines (44 loc) · 3.96 KB

Modern PHP

Modern PHP (8.2/8.3+) adds type-safety, expressiveness, and structural features that make secure code easier to write and harder to get wrong. This module covers the language features every professional PHP developer is expected to know today.

Learning Objectives

  • Use the modern type system (union, intersection, mixed, never) to make invalid states unrepresentable.
  • Model fixed domains with enums and immutable data with readonly.
  • Understand autoloading, namespaces, traits, and the SPL that structure real applications.
  • Handle failure correctly with exceptions and typed error handling.

Type System

  • Union-Types — accept one of several types on a single parameter or return.
  • Intersection-Types — require a value to satisfy several interfaces at once.
  • Mixed-Type — the explicit "any" type and why it weakens guarantees.
  • Never-Type — mark functions that always throw or exit.
  • Nullsafe-Operator — short-circuit method chains on null safely.

Classes & Objects

Functions & Control

  • Named-Arguments — pass arguments by name for clarity and optional-arg skipping.
  • First-Class-Callable-Syntax — reference functions and methods as values with (...).
  • Match-Expression — strict, expression-based branching (see Control Structures).
  • Generators — lazy iteration with yield for memory-efficient streams.
  • Fibers — cooperative, interruptible execution for async runtimes.

Structure & Runtime

  • Namespaces — organize code and prevent name collisions.
  • Autoloading — load classes on demand (foundation for Composer PSR-4).
  • Standard-PHP-Library-SPL — built-in data structures, iterators, and interfaces.
  • WeakMap — associate data with objects without preventing garbage collection.

Errors & Exceptions

  • Exceptions — the exception hierarchy and throwing/catching correctly.
  • Error-Handling — error vs exception, handlers, and declare(strict_types=1).

Labs & Exercises

Every note in this module ends with hands-on Exercises. Drill them, then apply the features in the security-focused labs where modern syntax pays off:

  • Lab-Insecure-Deserialization — enums and readonly value objects as a safe alternative to unserialize().
  • Lab-Building-a-REST-API — union/never types, first-class callables, and match in a real request handler.
  • Module challenge: refactor one procedural script from an earlier folder to use enums, constructor promotion, readonly properties, and match — then run it under PHPStan at max with zero errors.

Related