Bummer! You have been redirected as the page you requested could not be found.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Extending Object-Oriented PHP!
You have completed Extending Object-Oriented PHP!
Preview
To inherit ALL the abilities and attributes of another class, we use the keyword "extends ".
Key Concepts
- Use the Keyword extends
- You can only extend from one class at a time.
- A child class will "inherit" all the attributes and actions of a parent
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's jump right into code and
see what it takes to extend a class.
0:00
The classes for this project
are stored in a folder named classes.
0:04
In that folder is a listing
ListingBasic class.
0:08
This is the class we're going to extend.
0:11
So let's take a look at
what the class includes.
0:13
There are some private properties,
a constructor that sets those properties,
0:18
and the getters and setters for
each of those properties.
0:23
There's also a method named two array
0:25
that turns the object properties
into an associative array.
0:28
Let's create our new class
in the classes folder.
0:32
We'll name it ListingPremium.
0:37
To build a class, we use the keyword
class, followed by the name of our class.
0:47
To inherit the abilities and
0:53
attributes of another class,
we then use the keyword extends.
0:55
Followed by the name of the class
we wish to extend, ListingBasic.
1:01
We'll add the curly braces but
leave this new class empty.
1:07
Before we do anything else let's
try using this new child class.
1:12
Open classes Collection.php.
1:17
This is the main controller for
our application.
1:20
This class takes a connection
to a database on construction.
1:23
It also has a method for
selecting listings from the database.
1:27
This method calls the addListing method,
that adds a listing and
1:32
returns a listing object.
1:37
For right now, all we're going
to do is change the class used.
1:40
From ListingBasic to ListingPremium.
1:44
Before this will work I need to make this
new class available to the application.
1:50
Open inc, config, this file performs some
of the basic tasks for our application.
1:54
We start a session Include the classes,
connect to the database, and finally,
2:03
create a directory object by passing
the connection to our collection class.
2:08
Lets copy the listing basic include.
2:13
And we'll change it to ListingPremium.
2:19
Now lets preview this site in a browser.
2:25
Even though we didn't add any code
to our ListingPremium's class,
2:29
the site still functions exactly the same.
2:34
Let's go back to our ListingPremium class.
2:37
As you can see, this is in fact empty.
2:43
However, we use the extend keyword
to extend from ListingBasic.
2:47
ListingBasic is the parent class and
ListingPremium is the child class
2:53
that inherits all the abilities and
attributes of the parent.
2:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up