Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As previously mentioned, visibility of a property or a method is defined using a scope keyword: public, protected, or private. If a method or property is defined as public, it means that it can be both accessed, and properties can be changed by anything that can refer to the object. Sometimes that's a good thing. You may have no need to restrict access to a property or method. For example, public methods can be exposed as part of an application programming interface or API. As an abstract example, think of public like a picnic that everybody is welcome to join.
If defined as protected, members can only be accessed within the class itself, and by inherited and inheriting classes. Inheritance will be covered in a later video, but in short, a class can have all the properties and methods of another class. The end result is a superset of all the methods and properties of both classes. The class that is inheriting the methods and properties is referred to as the child, and the class from which the methods and properties are coming from is known as the parent. Therefore, a protected member from the parent would be accessible by the child.
However, regular code or a different class that was unrelated to the parent class would not be able to access the protected property or method. To continue with the picnic example, think it of as a company picnic where family members of the employees are allowed, but not the general public. When restricting visibility, protected is the most common scope restriction. Finally, if a method or property is defined as private, only the class that has the private members can access those methods or properties, despite whatever relationships between classes there may be.
With the picnic example, only company employees will be allowed; family members and the public are excluded. In regular practice, private visibility is used less often, as it can cause headaches and bugs that are created by humans writing code when dealing with class hierarchy. So, use it sparingly. Let's extend the address class by adding some protected properties. We will add three protected properties: a primary identifier for the table that will be accessed later, a time created, and a time updated. We are protecting them, as we do not want them to be accessed directly.
This is internal information that should not be manipulated by anything other than the address class and anything that's associated with it. A best practice to visually indicate that a method or property has been protected is to prepend the name with a single underscore (_) character. Open the Address class, then navigate to after the property declarations. We'll start with the primary key of an address. protected $_address_id. Next, when the record was created and last updated. protected $_time_created.
protected $_time_updated. Save the address class. Test out these new protected properties by adding the following line to the end of the demo: echo h2 Testing protected access. Echo. Double quotes this time. Address_ID ($address->_address_ID]. This will intentionally fail. Save, then refresh your browser. We'll get a fatal error about accessing the protected properties, which is just what we're expecting.
In this chapter, we've built upon object-oriented theory with some practical examples. Starting with the class definition, we added properties to store things like the city and street. We created a method to render the address in HTML, then we instantiated an object of class address, accessing and manipulating the contents. Finally, we restricted access by specifying the visibility scope. This resulted in a fatal error. So, in the next chapter, I'll demonstrate how to correctly access these protected properties.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100058 Viewers
56 Video lessons · 113115 Viewers
71 Video lessons · 81971 Viewers
131 Video lessons · 39327 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.