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!
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
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
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