A photo for the front page

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

Topics covered: Nullable typenullDefault value

The village photographer, in other words Odette's nephew, took a picture of last year's pumpkin under the moon. Madame Lou wants it on the front page. But not every article will have a photo: you need a property that can hold nothing.

Your mission

  1. Add a read-only photo property to Article, of type ?string, which is null by default. It holds the name of a file in the public/images/ folder.
  2. Give the festival article the photo 'citrouille.svg' (citrouille is French for pumpkin).
  3. In afficherArticle(), below the title, when the article has a photo, show <img class="photo" src="images/citrouille.svg" alt="…">, with the article's title as the alt text.

Reminder

null means “no value”. A type preceded by ?, like ?string, accepts a string or null. Before using a nullable value, check that it is there: if ($article->photo !== null). The alt text describes the image to anyone who cannot see it.

To replace null with a fallback value, the ?? operator saves you the if: $article->photo ?? 'no-photo.svg' is the photo if there is one, and 'no-photo.svg' otherwise. It also works for an array key that may be missing: $tableau['cle'] ?? ''.