Building Relationships3:32 with Alena Holligan
Classes are great but what if you need to create different classes that share many of the same features? PHP lets us make classes that inherit the abilities and attributes of another class.
[MUSIC] 0:00 Welcome, I'm Alena. 0:04 In this course we'll be extending from the knowledge learned in a previous course 0:06 to look at additional relationships in the structure on our code. 0:11 Laziness is one of the great virtues of a programmer, 0:15 we don't want to write the same code over and over again. 0:18 This is why one of the core concepts of object oriented programming 0:22 is aimed directly at reducing the amount of repetition that you have to do. 0:26 It's called inheritance. 0:31 At its simplest, it means we can give a child or 0:34 a subclass all the abilities and attributes of a parent or superclass. 0:36 This helps us to greatly reduce the amount of code we need to write. 0:42 Let's take these elephants for example. 0:47 Each of these elephants could be an instance of an elephant class. 0:49 Each object would simply set specific properties such as color and logo. 0:54 Both are created using the same pattern or class. 0:59 But what happens when we want to build a mammoth? 1:03 Not only have we changed the type of material that's used, but 1:07 we've also added tusks. 1:11 Continuing to modify the original pattern to add more features, can get complicated, 1:13 and make it more difficult to create our simple base elephant. 1:19 Then what happens when we want to add additional designs? 1:23 Such as an Elvis elephant. 1:26 Instead, we can keep our original pattern in our class, and 1:29 then extend that class by creating a child, or sub-class, which 1:32 includes all the attributes of the parent, while additionally adding features. 1:37 As another example let's take a bike. 1:43 We have properties such as frame color, number of gears, and current speed. 1:46 We also have methods that control things such as brake, change speed, and 1:51 change gears. 1:55 Now let's say we want to build an electric bike. 1:57 An electric bike is a regular bike with the addition of an electric drive system. 2:00 We would need additional properties for battery and 2:05 motor and a method to control the power such as change_power. 2:08 Instead of adding these properties and methods to the bike class we could create 2:13 a child class named electric bike, that extends the original bike class 2:18 by giving the child access to all the original properties and 2:23 methods while allowing the child to add these additional features. 2:27 In this course, we'll write code to extend a simple directory project. 2:32 If you would like to practice building the original directory, or 2:36 would like a refresher of things that are not covered in this course, 2:40 please check the notes associated with this video. 2:43 Let's take a look at the project. 2:46 The simple directory we'll be starting with has a list of PHP Conferences 2:49 with links to their website, email, and twitter. 2:53 We have the ability to edit and deactivate a listing. 2:56 We also have the ability to add a new listing. 3:03 ll We'be adding premium listings which include a description and 3:05 featured listings which also include a code of conduct. 3:11 These new listing types will be used on the listing page, and also for the forms. 3:15 Along with these features, we'll also add additional styling and navigation. 3:21
You need to sign up for Treehouse in order to download course files.Sign up