HomePHP Working with Private Properties to Protect PHP 5 Class Data
Working with Private Properties to Protect PHP 5 Class Data
Welcome to the second installment of the series, “Protecting the data of PHP 5 classes with member visibility.” It is made up of six comprehensive tutorials and provides you with a guide to help you get started using member visibility with PHP 5. It also complements the corresponding theoretical concepts with illustrative hands-on examples.
One of the most useful features that was introduced into the improved object model of PHP 5 is “member visibility.” It provides PHP developers with the ability to specify the level of access each data member of a class will have in the context of a given application. This helpful characteristic of the language allows the user to declare methods and properties of a certain class that will be available in the global scope via the “public” keyword. It also establishes more restrictive access levels by using the “protected” and “private” reserved words.
That being said, it’s time to refresh the concepts that were deployed in the last episode of the series, in case you haven’t had the chance to read it yet. Simply put, in that article, I developed a few basic hands-on examples aimed specifically at demonstrating how to declare and work with public and protected properties defined within a primitive data saving class. This came in handy for storing some simple strings in a specified text file.
In the first case, the properties of the class were declared public, meaning they could be directly accessed from outside the class in question. In the second case, the same properties were defined as protected, implying that they could only be retrieved from inside the originating class and from their eventual subclasses as well.
So to summarize, I've already covered the use of public and protected class properties in PHP 5. Thus, the next step in this learning process will consist of showing you how to work with private properties. Sounds pretty interesting, right?
So, let’s not waste more time with preliminaries and see how to use private properties when working with PHP 5 classes. Let’s get going!