Siblings and Grandchildren4:12 with Alena Holligan
We can create as many children as we want. We can also add children to those children for an infinite number of generations. Each subsequent generation gaining the attributes and actions of ALL generations before.
Children can only extend ONE parent at a time. Classes can be extended as many times as desired.
Siblings share the same attributes and abilities of the parent, but they do not share any attributes or abilities added in another sibling.
Grandchildren share the attributes and abilities of all parents in the hierarchy.
[MUSIC] 0:00 So far, we've built a single parent with a single child. 0:04 This child was able to share and override the functionality of the parent, 0:08 allowing us to reduce the amount of repetition in our code. 0:14 But we don't need to stop there. 0:18 We can create as many children as we want, and each of these children will inherit 0:21 all of the abilities and attributes of the parent class. 0:26 However, they will not have any of the added abilities and 0:31 features of the other children. 0:34 This is called a Sibling Relationship, because both classes have the same parent. 0:37 You could of course add the same features to another child, 0:42 but there's a better way. 0:45 When you want to create a new class that has all the abilities and 0:48 attributes of a child, as well as the parent, 0:52 child classes can be extended further to create grandchildren. 0:55 You may continue to add children for an infinite number of generations. 1:00 Each subsequent generation gaining the attributes and 1:04 actions of all generations before. 1:08 The only limitation is that you may only extend from one parent at a time. 1:11 Let's go back to our code and see this in action. 1:16 Let's create a second child of ListingBasic. 1:20 We'll call this ListingInactive. 1:25 And we'll extend, ListingBasic. 1:38 This will be a sibling of ListingPremium. 1:47 Without adding any code, this class will have all the abilities and attributes of 1:51 ListingBasic, and can be swapped out for that parent class just like we did before. 1:56 However, this class will not know about the description feature, so 2:02 we will not be able to use that functionality. 2:06 Let's include this new class in our config, 2:09 Now we can switch out our test object to use the ListingInactive. 2:22 Open index.php. 2:26 Now let's preview this in a browser. 2:38 Because we're passing the description into our construct, 2:40 it just ignores the description, because ListingInactive does not use that. 2:44 Let's go back and create another class. 2:49 This one will be a child of ListingPremium. 2:51 We'll call this ListingFeatured, 2:55 And it extends ListingPremium. 3:07 This class will inherit all the abilities and 3:17 attributes of not only the ListingPremium, but also the ListingBasic. 3:20 Once more, we need to include this class in our config. 3:27 Now we can add another test. 3:36 Now let's preview in the browser. 3:49 Let's view the page source and increase the font size. 3:54 Even though ListingFeatured doesn't have the set description methods or 4:02 attributes defined, it is using the inherited behaviors and attributes. 4:07
You need to sign up for Treehouse in order to download course files.Sign up