Object-Oriented PHP and Composer: Code Like a Pro

Object-Oriented PHP and Composer is an intermediate-level course of 6 lessons (about 3 h 13 min), with 19 exercises and 18 quiz questions. Prerequisites: PHP Fundamentals. Exercises are run and checked in a server-side sandbox.

  • PHP
  • Intermediate
  • 6 lessons
  • 3 h 13 min
  • 19 exercises
  • Exercises run in a server sandbox

The course in brief

This course takes you from procedural PHP to professional PHP. You model business objects with classes, encapsulation, inheritance, interfaces and enums, handle errors with exceptions, then structure a project with namespaces, Composer and PSR-4 autoloading. It is the essential foundation before Laravel or Symfony.

What you will be able to do

  • Design typed classes with constructor promotion, readonly properties and encapsulation
  • Use inheritance, abstract classes, interfaces and traits appropriately
  • Model closed sets of values with enums and value objects with named constructors
  • Handle errors with SPL and domain exceptions
  • Structure a project with namespaces, Composer and PSR-4 autoloading

Syllabus: 6 lessons

  1. 01

    Classes and Objects

    15 minFree preview
    • Declare a class with typed properties, a constructor and methods
    • Instantiate objects and call their methods with ->
    • Use constructor promotion and readonly properties

    Classes and objectsConstructor and promotionReadonly properties

  2. 02

    Encapsulation and Inheritance

    18 min
    • Protect an object's state with private and protected and control methods
    • Create a child class with extends, parent:: and method overriding
    • Use abstract classes, final and the #[\Override] attribute

    Encapsulation and visibilityInheritanceAbstract classes

  3. 03

    Interfaces, Polymorphism and Traits

    18 min
    • Define a contract with an interface and implement it in several classes
    • Write polymorphic code that depends on an interface rather than a concrete class
    • Reuse cross-cutting code with a trait

    InterfacesPolymorphismTraits

  4. 04

    Static Members, Constants and Enumerations

    16 min
    • Distinguish instance members from static members, and use self:: and static::
    • Declare typed class constants (PHP 8.3)
    • Model a closed set of values with an enum, with methods and associated values

    Static membersClass constantsEnumerations

  5. 05

    Handling Errors with Exceptions

    16 min
    • Throw and catch exceptions with throw, try, catch and finally
    • Pick the right SPL exception and create your own exception classes
    • Organize error handling: fail early, catch at the right level

    ExceptionsCustom exceptions

  6. 06

    Namespaces, Composer and PSR-4 Autoloading

    20 min
    • Organize your code with namespaces and the use statement
    • Initialize a Composer project, install dependencies and understand composer.json and composer.lock
    • Configure PSR-4 autoloading to load classes without require

    NamespacesComposerPSR-4 autoloading

Final project

Online Shop Order Engine

Build, without a framework, the business core of a shop as a Composer project: value objects (Prix in cents, Email), entities (Produit, Commande, LigneCommande), a StatutCommande enum with its transitions, a MoyenDePaiement interface with two implementations and business exceptions (StockInsuffisant, TransitionInterdite). A public/index.php script simulates an order going through and displays a summary; a few PHPUnit tests check the main rules.

Deliverables

  • A Composer project with PSR-4 autoloading (App\ → src/, Tests\ → tests/) and a committed composer.lock
  • Classes, interfaces, enums and exceptions in separate files following PSR-4
  • A public/index.php demonstration script
  • At least five PHPUnit tests run with composer test

Assessment criteria

  • Business invariants are guaranteed by the classes (encapsulation, readonly, validation in constructors)
  • The code depends on interfaces and receives its dependencies by injection
  • Statuses are modeled with an enum and forbidden transitions throw a business exception
  • Errors are handled with precise exceptions, never swallowed
  • The Composer structure and namespaces are correct; the tests pass

Frequently asked questions

Do I need to know OOP to learn Laravel or Symfony?

Yes. These frameworks are built entirely on classes, interfaces, dependency injection, namespaces and Composer: this course gives you the necessary basics.

What is Composer?

Composer is the dependency manager for PHP. It installs the libraries declared in composer.json, locks their versions in composer.lock and generates an autoloader to load classes automatically.

Which recent PHP features are covered?

Constructor promotion, readonly properties and classes, enums, the match expression, named arguments, typed class constants and the #[\Override] attribute from PHP 8.3, as well as clone with property changes and the #[\NoDiscard] attribute from PHP 8.5.

Do the exercises require an installation?

The code exercises run in your browser with PHP 8.5. Only the hands-on task on Composer requires a local installation of PHP and Composer.

This course, told for your job

Say what you want to be able to do: “Object-Oriented PHP and Composer” takes its place in your path, with examples from your field. The concepts stay those of the course.

The preview is free, and you can take the courses one at a time without paying. The path only contains lessons from the catalogue.