The Article class

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

Topics covered: ClassObjectConstructorNamed arguments

A typo has slipped into the fourth article: 'titr' => … instead of 'titre' => …. Look at the preview: the soup contest came out without a title. With arrays, PHP gets by with a warning (“Undefined array key”) and prints the page anyway, title or no title. Madame Lou wants an article template that nobody can fill in the wrong way: a class.

Your mission

  1. Declare an Article class whose constructor receives, in this order: string $titre (title), string $auteur (author), string $rubrique (section), string $texte (text) and bool $une (front page), which is false by default. Turn them into public properties with constructor promotion: write public string $titre right in the constructor.
  2. Replace the four arrays with objects: new Article(titre: '…', auteur: '…', …).
  3. Change afficherArticle() so that it receives an Article: $article->titre replaces $article['titre'].

The front page must not change.

Reminder

A class is a blueprint; an object is a thing built from that blueprint with new. The constructor __construct() runs when the object is created. Its parameters preceded by public become properties, which you read with the arrow ->. A typo in a named argument is an error, and PHP reports it right away.