HomePHP Implementing Property Overloading in PHP 4
Implementing Property Overloading in PHP 4
The object-oriented paradigm includes the ability to overload classes; not surprisingly, this is possible in PHP. In this first part of a series, you will learn how to implement class overloading in PHP 4, specifically using the “overload()” function, which comes in handy for triggering “__set()” and “__get()” methods when a property access is overloaded.
If you’ve been working with object-oriented programming in PHP for a considerable time, you probably have a decent grounding in building up classes, defining their methods and properties, as well as implementing several types of object interaction, including aggregation and composition in cases where these techniques are applicable. Additionally, you’ll surely have started using some of the most important foundations of object-based development, such as inheritance and polymorphism.
To return to mastering all the concepts that I deployed before, perhaps you consider yourself an experienced PHP developer that has explored and understood the nitty-gritty of working with objects during the development of different applications. However, luckily for you and me, the object-oriented paradigm always has some additional exciting aspects that might be pretty interesting to expand our overall knowledge of PHP.
That’s precisely the case with the overloading of classes, since PHP comes with a fairly handy support for overloading both method calls and property access of objects, something that can be truly useful when used in a clever way. Certainly, I don't mean that the implementation of class overloading in PHP is as powerful as you’ll find if you work with C++ or Python, but it definitely can help in the development of more robust code, particularly when you want to combine it with the advantages offered by inheritance.
After introducing the topic, allow me to tell you what you’ll learn during the course of this series, so you won’t suffer the effects of unexpected surprises. In these articles, I’ll explain the basics of class overloading, starting with the application of the “overload()” function in PHP 4, in conjunction with using the “_set()”, __get()” and “__call()” methods, accompanied of several practical examples, so you’ll have a clear idea of how to overload your classes. Also, I’ll cover class overloading in PHP 5, which offers native support for overloading methods and properties through the built-in methods that I mentioned before.
With the preliminaries out of our way, let’s start learning more about how to implement class overloading in PHP. Let’s go!