Sections

Exercise 3 of 9 in chapter 2 · 100 XP · 5 objectives checked by tests

Topics covered: Enummatch

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

  1. Declare a Rubrique (section) enum with string values and three cases: FeteDuVillage ('village-fair'), Meteo ('weather') and FaitsDivers ('news-in-brief').
  2. Give it a libelle(): string (label) method that uses match to return the readable name: “Village fair”, “Weather”, “News in brief”.
  3. The rubrique property of Article becomes a Rubrique, and each article gets its own: rubrique: Rubrique::Meteo.
  4. 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.