Modern OOP Checkpoint

Modern OOP Checkpoint

Playground Checkpoint

Refactor the booking classes with an enum, a property hook, asymmetric visibility and late static binding — the expected output tells you when it's right.

30–40 min

The Challenge

Before launch, Marcus wants the booking classes in modern PHP: no magic strings, no getter-and-setter boilerplate, no data that outside code can change, and no deprecation warnings in the log.

Open the starter in the PHP Playground with the button below and run it. It prints a deprecation, the wrong class, a phone number full of spaces, "Confirmd (confirmd)", the total without the group discount — and it lets outside code change the status. Fix the classes, not the counter-screen lines.

What You Need to Do

  • Run the starter and match each wrong line of output to a chapter of this Act.
  • Replace the status string with a string-backed enum BookingStatus (Pending, Confirmed, Cancelled) with a label() method; statusText() shows the label and the value (chapter 2).
  • Make Booking::$status readable everywhere but writable only inside the class (chapter 7).
  • Remove Customer's getter and setter: a property hook on $phone removes all whitespace on every write (chapter 7).
  • Declare the note property so there's no dynamic property (chapters 1 and 7).
  • Make Booking::create() return the class it was called on (chapter 4).
  • Don't change the lines under "The counter screen". The output must match the expected output exactly, with no warnings or deprecations.
Stuck? A Few Hints
  • "Confirmd" comes from a typo in confirm(). With an enum, BookingStatus::Confirmd wouldn't even run (chapter 2).
  • public private(set) BookingStatus $status = BookingStatus::Pending; — the counter screen's assignment then fails with exactly the expected message (chapter 7).
  • The constructor assigns $this->phone = $phone;, and the set hook cleans it (chapter 7).
  • new self builds a Booking even when called as GroupBooking::create(); new static builds the caller, and the return type becomes static (chapter 4).

Ready to Build It?

Opens the Playground, right in your browser — nothing to install.

Open Playground

A correct run prints six lines: GroupBooking, 07700900456, "Confirmed (confirmed)", "Total: £135.00", the private(set) refusal and "Note: Stag party" — with no deprecation or warning. The starter prints a dynamic-property deprecation, "Booking", the phone with spaces, "Confirmd (confirmd)", "Total: £150.00" and "Status changed from outside!".

Before You Move On

If your output matches, BookBay's booking classes say exactly what they mean: a status can only be a real status, a phone number is always clean, and nothing outside can change what it shouldn't. That completes object-oriented PHP — both the classic features every language shares and the modern ones that make PHP 8.5 different. Next: namespaces, autoloading and Composer, so all these classes can live in their own files and load themselves.