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
all the same functions and utilize the same properties as the parent class.
Additionally a child may add it's own attributes and abilities.
This is where extending comes in.
There are many additional features that we could add to extend the premium listing,
such as a description, logo, dates, code of conduct and call for papers.
For this project I’m going to add a description, but go ahead and
add your own features when we’re done.
The first thing we need to do is create a new property with it's getters and
Let's copy the title getter and setters from the ListingBasic class.
Now we can replace title with description.
Let's test this out on our index page.
We'll add a new object named test, and we'll set this = new ListingPremium().
Now let's move our display out of the for each loop.
Now we can add a description to the test on the index page.
We'll set this to My description.
Now, let's check this out in the browser.
Whoops, I forgot to change listing to test.
Let's try that again.
Great, our test listing has the new description property.
Let's add a little more.
I want to allow my description to accept certain tags.
There are many different ways to do this, but
I'm going to use the strip tags function.
So let's check out the documentation.
You can use the optional second parameter to specify tags,
which should not be stripped.
You specify a string with a simple form of the tags you wish to allow.
Let's create a new protected property.
and we'll set this to
That will give us a decent list.
Now we need to change the setter.
We've been using the filter_var to sanitize the information coming in.
This allows us to safely accept user data.
Instead of filter_var,
we're going to use the strip_tags function to strip all but a few simple tags.
For the second parameter,
we specify our allowed tags $this->allowed_tags.
Let's go back to index.php, and add some tags to our description.
Good tags, and-
Now, let's test this in a browser again.
You can see that the description left the B tags but removed the link.
Because the A tag is not one of the allowed tags.
Remember, the set description method is specific to our listing premium class and
our listing basic class will not be affected.
Let's try changing this test to use our ListingBasic class.
Now we get a call to undefined method on our ListingBasics class,
because it does not know about the set description method that
only applies to a listing premium object.
Let's change this back to listing premium.
You need to sign up for Treehouse in order to download course files.Sign up