Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we will look at class scope and how a child can OVERRIDE existing properties and methods of a parent.
Class Scope
The Visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private.
- public - Class members declared public can be accessed everywhere.
- protected - Members declared protected can be accessed only within the class itself and by inheriting classes.
- private - Members declared as private may only be accessed by the class that defines the member.
NOTE: we can have two private properties with the same name when a parent and child both define the same private property. This is because the child class does not know about the parents private property and therefor creates it's own.
Final Keyword
If you want to be certain that a child class cannot override a method of a parent, you by prefixing the definition with the keyword final.
final public function myMethod() {}
Also, if the class itself is being defined final then it cannot be extended.
final class BaseClass {}
Note: Properties cannot be declared final, only classes and methods
-
0:00
In the last video, we added an additional property and method to our child class.
-
0:06
A child class may also override existing properties and
-
0:09
method as long as the parent permits that change.
-
0:13
This is part of the class scope.
-
0:16
Just like we specify which properties and methods can be accessed outside
-
0:21
the class using public visibility, we can also set certain properties and
-
0:26
methods to be accessible to a child class using protected visibility.
-
0:32
Let's take a closer look at the scope of the child and
-
0:34
how we can override the parent.
-
0:38
The major difference between a basic listing and
-
0:40
a premium listing is the status.
-
0:43
So we need to change the default value of the status property.
-
0:47
Let's copy that line from the ListingBasic class.
-
0:58
And change it to premium.
-
1:00
Back in index.php, let's call the getStatus method on our test object.
-
1:20
Now let's take a look in a browser.
-
1:26
We can see that we have two private status properties.
-
1:31
The first status property has a premiums value,
-
1:35
and the second status property has a basic value.
-
1:39
How can we have two properties with the same name?
-
1:42
Notice that the basic status shows ListingBasic.
-
1:47
This is because the property is private.
-
1:50
ListingPremium doesn't know anything about the status property
-
1:54
of the ListingBasic class, so it creates it's own property.
-
1:59
This can get really confusing really quickly.
-
2:03
Also note that when I called the getStatus method,
-
2:06
it pulled the status of the listing basic class.
-
2:11
That's because getStatus is a method from the ListingBasic class,
-
2:16
and as such can access the private property of that class.
-
2:22
If you don't want the child to access a property directly,
-
2:25
you should continue to keep the visibility private.
-
2:28
The properties that do not have a default value should remain private,
-
2:33
giving access only through getters and setters.
-
2:36
When you want to be able to directly access a property from a child class,
-
2:40
you should set the visibility to protected.
-
2:54
This keeps the property protected from outside the class just like private does,
-
2:59
but it will allow a child class to access that property.
-
3:03
Let's preview this again.
-
3:07
Now we have a single status property and it's set to premium.
You need to sign up for Treehouse in order to download course files.
Sign up