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, 0:00 where we use the parent. 0:04 Let's take a closer look at the result. 0:06 Open index.php. We'll start by adding a var_dump of 0:08 the listings within the foreach loop. 0:12 Let's preview this in the browser. 0:20 Lets view the page source, and increase the font size. 0:25 This shows us that the object itself is a listing premium object, but 0:29 its variables are taken from the listing basics class. 0:36 Let's take a look at a couple more functions that will tell us about 0:45 our class. 0:48 Var_dump get_class, 0:53 This will give us the name of the object's class. 1:03 We can also check if this object is of a certain type. 1:06 We'll do a var_dump is_a, pass the listing, 1:09 And then the class we wish to compare, ListingBasic. 1:18 Let's preview this again. 1:24 Now we see that it tells us that our class is a listing premium, but 1:28 our check with the is a function, returns true. 1:32 That's because listing premium is a child, and therefore a type of 1:36 ListingBasic, and can be used wherever ListingBasic is used. 1:41 We could also see in the status of these listings, 1:46 that some of them are premium, And some of them are basic. 1:48 Let's go back to our collection class and use ListingBasic for 1:55 our basic listings, and ListingPremium for our premium listings. 1:59 In the addListing method, we need to check the status. 2:08 If isset status, 2:16 And that same status, 2:23 Is equal to premium, then we're going to use ListingPremium. 2:31 If not, else, we're going to use listingBasic. 2:38 Let's check this out in the browser once more. 2:51 Now the listings that have a status of basic, are of a ListingBasic class. 2:55 And those that have a status of premium, are of a ListingPremium class. 3:03 We're ready to actually extend our child class in more than just name 3:08
You need to sign up for Treehouse in order to download course files.Sign up