Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Using Multiple Classes3:13 with Alena Holligan
When using multiple classes, there are several functions in PHP that will allow us to learn about those classes.
- get_class() - Returns the name of the class of an object
- is_a() - Checks if the object is of this class or has this class as one of its parents
- get_object_vars() - Gets the properties of the given object
You could also try some additional class/object functions
In the last video, we saw that we could easily swap out our new child class,
where we use the parent.
Let's take a closer look at the result.
Open index.php. We'll start by adding a var_dump of
the listings within the foreach loop.
Let's preview this in the browser.
Lets view the page source, and increase the font size.
This shows us that the object itself is a listing premium object, but
its variables are taken from the listing basics class.
Let's take a look at a couple more functions that will tell us about
This will give us the name of the object's class.
We can also check if this object is of a certain type.
We'll do a var_dump is_a, pass the listing,
And then the class we wish to compare, ListingBasic.
Let's preview this again.
Now we see that it tells us that our class is a listing premium, but
our check with the is a function, returns true.
That's because listing premium is a child, and therefore a type of
ListingBasic, and can be used wherever ListingBasic is used.
We could also see in the status of these listings,
that some of them are premium, And some of them are basic.
Let's go back to our collection class and use ListingBasic for
our basic listings, and ListingPremium for our premium listings.
In the addListing method, we need to check the status.
If isset status,
And that same status,
Is equal to premium, then we're going to use ListingPremium.
If not, else, we're going to use listingBasic.
Let's check this out in the browser once more.
Now the listings that have a status of basic, are of a ListingBasic class.
And those that have a status of premium, are of a ListingPremium class.
We're ready to actually extend our child class in more than just name
You need to sign up for Treehouse in order to download course files.Sign up