One file per class

Exercise 1 of 4 in chapter 3 · 100 XP · 5 objectives checked by tests

Topics covered: require_once__DIR__Code organization

public/index.php is now more than 150 lines long, and Madame Lou gets lost in it. To find the Annonce class, she has to scroll through the whole page. In a PHP project, each class has its own file, kept in src/.

Your mission

  1. Move each declaration into its own file in src/, named after it: src/Publication.php, src/Rubrique.php, src/Auteur.php, src/Article.php, src/Annonce.php and src/Sommaire.php. The files are waiting for you, empty.
  2. In public/index.php, load them with require_once, starting from __DIR__, following the pattern require_once __DIR__ . '/../src/Article.php';.
  3. Mind the order: a class cannot implement an interface that is not loaded yet.

The front page does not change, but the code becomes readable.

Reminder

require_once runs another PHP file, as if it were pasted right there, and only once, even if it is asked for twice. __DIR__ holds the folder of the current file: a path built from it works wherever the script is run from.