Specifying the visibility scope
Video: Specifying the visibility scopeAs 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.
Viewers: in countries Watching now:
Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.
- Historical overview of object-oriented PHP
- Defining classes
- Creating a method/object context with $this
- Accessing classes without instantiation
- Creating a database class
- Extending and abstracting classes
- Cloning and comparing objects
- Error handling with exceptions
- Implementing design patterns, such as the factory and strategy patterns
Specifying the visibility scope
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.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- Q: I'm not seeing warnings or errors in my environment like the video; why not?
- A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.
The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.
error_reporting(E_ALL | E_STRICT);
Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of
error_reporting = E_ALL | E_STRICT
then change display_errors = to
display_errors = On
Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:
php_value error_reporting 32767
php_value display_errors 1
If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.
Q: This course was updated on 4/10/2013. What changed?
A: The author rerecorded some of the tutorials to add more background information and better graphics.
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.