Extending a Method4:27 with Alena Holligan
Sometimes you have a method that you want to keep, but you just want to add a little more functionality. Once again, PHP gives us a way to keep the original method while still adding additional code.
The Scope Resolution Operator, the double colon (::), is a token that allows access to static, constant, and overridden properties or methods of a class. You can use this operator when Extending the method of a parent class.
will call the constructor from the parent class. You can then add your own code either before or after the call to the parent method.
Sometimes you have a method that you want to keep, but 0:00 you just want to add a little more functionality. 0:03 Once again, PHP gives us a way to keep the original method while still adding code. 0:07 In the original listing basic class, 0:13 the constructor method calls a set values method. 0:16 Which, as its name implies, sets each property value. 0:19 We need the set values method to set the description as well, but 0:24 only for the ListingPremium class. 0:29 Let's copy the method and paste it into our premium class. 0:31 At the end, let's copy status and change it to description. 0:38 SetDescription, we now have a set values method in our premium 0:52 class that overrides the method in the basics class. 0:57 Let's go back to our test and add the description on construction. 1:01 This is an array, so we say description, 1:08 = My description. 1:17 And let's preview this again. 1:27 As we can see, the description is still set. 1:30 This works, but 1:35 it's a lot of repeated code which is something that we're trying to avoid. 1:36 Instead of setting all the properties that were set in the basic class, 1:41 I can call the original method from the parent class. 1:46 parent::setValues and we pass the data. 1:53 The code from the parent method is called wherever I place this MethodCall. 2:01 So if I wanted to add my new code before the original method, 2:06 I could place the call below my code. 2:10 The last class update is to our collection. 2:12 We need to update our statuses. 2:15 We need to add premium. 2:16 This will update the status drop-down from our form and 2:21 also control the order in our select statement. 2:24 So make sure that you add it as a first element. 2:27 Now that the classes are ready, we just need to add the description to our form. 2:30 Open views, form_listing. 2:34 Let's duplicate the web site group. 2:38 We'll remove the input tag, 2:50 because we're going to wanna include a text area instead. 2:52 Not all listings will have the get description method, only premium listings. 3:14 So before we can get the description, 3:20 we need to check if the listing is of the class listing premium. 3:22 Or has this class as one of its parents. 3:27 If (is_a($listing, 3:34 'ListingPremium')). 3:39 This will allow us to know if we'll be able to use the getDescription method. 3:45 Let's test upgrading a listing to premium. 4:00 Let's decrease this font size. 4:05 We get an error. 4:18 No such column allowed tags. 4:20 Let's take a closer look at exactly what Update is doing. 4:23
You need to sign up for Treehouse in order to download course files.Sign up