Chapter 2 of the Learn PHP 8 from scratch track
The newsroom gets organized
9 exercises · 1,100 XP · about 3 h 15 min
ClassObjectConstructorNamed argumentsreadonlyVisibilityMethod$thisEnummatchCompositionObject inside an objectNullable typenullDefault valueInterfaceimplementsPolymorphismProperty hookprivate(set)Virtual propertyArrow functionfnarray_filter()count()instanceofarray_map()array_unique()implode()
The chapter's exercises
-
The Article class
Exercise 1 · 100 XP · 5 objectives checked by tests
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.
ClassObjectConstructorNamed arguments
-
A title that stays put
Exercise 2 · 100 XP · 6 objectives checked by tests
Disaster: the front page says the festival is canceled! Léo has struck again, and one line was all it took to change a title after the fact. A published article should not move anymore, and nobody outside the class has any business touching its text.
readonlyVisibilityMethod$this
-
Sections
Exercise 3 · 100 XP · 5 objectives checked by tests
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.
Enummatch
-
Who wrote this?
Exercise 4 · 100 XP · 5 objectives checked by tests
Gaston finally agreed to sign the article about the mayor's cat. Readers, for their part, want to know who is talking to them. Madame Lou would like each article to be signed with the name and the role of its author: “By Gaston, village constable”.
CompositionObject inside an objectMethod
-
A photo for the front page
Exercise 5 · 100 XP · 4 objectives checked by tests
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.
Nullable typenullDefault value
-
Articles and classified ads
Exercise 6 · 150 XP · 5 objectives checked by tests
Madame Lou has an old postwoman's bike to sell, and she is not the only one: classified ads are piling up at the post office. An ad is not an article: no author, no standfirst, but a price and a contact. And yet the front page has to show both, without wondering which is which.
InterfaceimplementsPolymorphism
-
A price that holds up
Exercise 7 · 150 XP · 5 objectives checked by tests
Léo has struck again. He listed his old broken bike in the classified ads, at a price of -€5: “I'll pay you to take it away!” Madame Lou did not laugh. A price must never be negative, whatever is written in new Annonce(...). And ads at €0 should show “Free” rather than “€0”.
Property hookprivate(set)Virtual property
-
The table of contents
Exercise 8 · 150 XP · 4 objectives checked by tests
Madame Lou finds the front page a bit long to skim before her mail round. She wants one line at the very top that sums up the issue: “In this issue: 4 articles and 1 classified ad.” And no way is she counting again by hand for every issue: PHP knows how to filter and count an array.
Arrow functionfnarray_filter()count()instanceof
-
Bylines
Exercise 9 · 150 XP · 4 objectives checked by tests
Gaston is miffed: the table of contents counts his articles, but doesn't say who wrote them. So Madame Lou adds the bylines: “In this issue: 4 articles and 1 classified ad, by Madame Lou, Gaston, Odette.” Gaston wrote two articles, but his name appears only once.
array_map()array_unique()implode()