HomePHP Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
It’s quite probable that you want to see what happens when a private property defined by a parent is retrieved from inside a child class, or if it’s possible to access it via an “accessor” method. Therefore, in the next few lines, I’m going to develop a bunch of practical examples that will help you grasp how to work with private properties in these situations.
Like many other robust, object-oriented languages available nowadays, PHP 5 comes equipped with a handy feature called "member visibility." This permits you to explicitly specify the level of access to the methods and properties of a given class. It's possible to declare data members public, protected, and private entities respectively, which helps to establish different grades of protection.
However, it's possible that you're a PHP developer who's just starting to use the object model of PHP 5 and you want to learn how to use member visibility when building your own classes. If this is true, then you've come to the right place, since in this series of articles, you'll be provided with the right pointers to help you start coding public, protected, and private data members within your neatly-crafted classes.
And now that I've mentioned the series, you'll certainly recall that in the last article, I explained how to work with a couple of private properties that partially comprised the API of a basic class, which is useful for saving simple strings to a specified text file.
With that class available for testing purposes, I demonstrated how the PHP interpreter triggered a fatal error each time the private properties in question were accessed from outside of the originating class. Nevertheless, there are still some additional aspects concerning the use of private properties within PHP 5 classes that need to be covered.
Now, let's get rid of the preliminaries and continue learning useful things about using private properties with PHP 5 classes. Let's get going!