Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we will create a second child while we look at more ways to override methods.
Siblings
We cannot call a private property of a parent directly. We have to make sure we call the parent getter when overriding a method in a child or we may hit an infinite loop.
public function getTitle()
{
return '<strike>' . parent::getTitle() . '</strike>';
}
-
0:00
We have our second child, ListingInactive.
-
0:03
For this class we're going to rewrite the get method so
-
0:07
that the only data we return is the title and status.
-
0:11
We also want to update the status like we did for ListingPremium, so
-
0:15
let's start there.
-
0:21
Protected $status = 'inactive',
-
0:26
now we're ready to override our getters,
-
0:32
public function getWebsite(), and
-
0:38
we're just going to return nothing.
-
0:46
We'll do the same thing for email and Twitter.
-
1:01
For the title,
-
1:02
instead of just returning the title, let's return the title with a strikethrough.
-
1:26
We can't access the title directly, and since we're rewriting getTitle,
-
1:32
if we try to call this getTitle we'll hit an infinite loop.
-
1:37
We could change the visibility of the property, but
-
1:39
that's not really what we want to do.
-
1:41
Instead, we'll call the parent method just like we did
-
1:44
when we extended the set values.
-
1:50
GetTitle, now we need our collection to use
-
1:55
this new class, open classes, Collection.
-
2:01
We've already been using the inactive status just not given it its own class, so
-
2:06
we don't need to update the statuses.
-
2:08
The ad listing method determines which listing class we use.
-
2:17
Let's add another conditional.
-
2:27
If the status is inactive,
-
2:33
then we use the inactive class.
-
2:40
Let's preview this in the browser.
-
2:43
If we choose Inactive we now see that our title is crossed out and
-
2:49
we don't have any other details, but let's update the display.
-
2:59
Case inactive.
-
3:15
Great.
You need to sign up for Treehouse in order to download course files.
Sign up