Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Child Features5:18 with Alena Holligan
A child class will perform all the same functions and utilize the same properties as the parent class. Additionally, a child may add its own attributes and abilities.
Remember that the parent class will NOT know about any properties or methods added to the child.
- strip_tags() - Strip HTML and PHP tags from a string. You can use the optional second parameter to specify tags which should not be stripped.
- trim() - Strip whitespace (or other characters) from the beginning and end of a string
- 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
Additional Features You Could "extend" a Listing
- code of conduct
- call for papers
As a child of listening basic, the listing premium class will perform 0:00 all the same functions and utilize the same properties as the parent class. 0:04 Additionally a child may add it's own attributes and abilities. 0:09 This is where extending comes in. 0:14 There are many additional features that we could add to extend the premium listing, 0:16 such as a description, logo, dates, code of conduct and call for papers. 0:22 For this project I’m going to add a description, but go ahead and 0:27 add your own features when we’re done. 0:31 The first thing we need to do is create a new property with it's getters and 0:34 setters. 0:37 Private $description;. 0:38 Let's copy the title getter and setters from the ListingBasic class. 0:43 Now we can replace title with description. 0:55 Let's test this out on our index page. 1:10 We'll add a new object named test, and we'll set this = new ListingPremium(). 1:16 Now let's move our display out of the for each loop. 1:25 Now we can add a description to the test on the index page. 1:35 We'll set this to My description. 1:42 Now, let's check this out in the browser. 1:49 Whoops, I forgot to change listing to test. 1:53 Let's try that again. 2:03 Great, our test listing has the new description property. 2:07 Let's add a little more. 2:12 I want to allow my description to accept certain tags. 2:14 There are many different ways to do this, but 2:17 I'm going to use the strip tags function. 2:19 So let's check out the documentation. 2:21 You can use the optional second parameter to specify tags, 2:31 which should not be stripped. 2:35 You specify a string with a simple form of the tags you wish to allow. 2:38 Let's create a new protected property. 2:43 Protected $allowed_tags, 2:48 and we'll set this to 2:54 for 2:58 emphasis, 3:05 That will give us a decent list. 3:12 Now we need to change the setter. 3:20 We've been using the filter_var to sanitize the information coming in. 3:23 This allows us to safely accept user data. 3:27 Instead of filter_var, 3:29 we're going to use the strip_tags function to strip all but a few simple tags. 3:31 For the second parameter, 3:42 we specify our allowed tags $this->allowed_tags. 3:45 Let's go back to index.php, and add some tags to our description. 3:52 Good tags, and- 4:03 Bad tags. 4:20 Now, let's test this in a browser again. 4:25 You can see that the description left the B tags but removed the link. 4:33 Because the A tag is not one of the allowed tags. 4:37 Remember, the set description method is specific to our listing premium class and 4:41 our listing basic class will not be affected. 4:46 Let's try changing this test to use our ListingBasic class. 4:49 Now we get a call to undefined method on our ListingBasics class, 5:01 because it does not know about the set description method that 5:05 only applies to a listing premium object. 5:09 Let's change this back to listing premium. 5:13
You need to sign up for Treehouse in order to download course files.Sign up