Madame Lou is worried: sooner or later, Gaston will file his storm article under “Wheather”, and Odette hers under “village Fayre”. A section typed by hand always ends up with ten spellings. The gazette only has three, and PHP knows how to represent a closed list of choices: an enum.
Your mission
- Declare a
Rubrique(section) enum withstringvalues and three cases:FeteDuVillage('village-fair'),Meteo('weather') andFaitsDivers('news-in-brief'). - Give it a
libelle(): string(label) method that usesmatchto return the readable name: “Village fair”, “Weather”, “News in brief”. - The
rubriqueproperty ofArticlebecomes aRubrique, and each article gets its own:rubrique: Rubrique::Meteo. - In
afficherArticle(), show the section's label, still in capital letters.
Reminder
An enum declares every possible value of a type, and no other. You cannot misspell Rubrique::Meteo without PHP telling you. Like a class, an enum can have methods. Inside a method, $this is the case you call it on: for Rubrique::Meteo->libelle(), $this is Rubrique::Meteo. self, on the other hand, means the whole Rubrique type, not one case. So match ($this) compares the current case with each arm (Rubrique::Meteo => 'Weather', which you can also write self::Meteo inside the enum), and returns the result of the first one that matches. Since all three cases are covered, there is no need for a default arm.