Assuming that you already understand the logic for triggering the __set() and __get() methods individually, combining them into the same class is just a matter of including the signature for each of them within the class youre going to use. After adding these two methods, the definition of the DataSaver class looks like this: class DataSaver{ At this point, the above class exposes the __set() and __get() method conjunctly, which comes in very handy for being triggered when overloading two property accesses. This process is illustrated below: // example of __set() and __get() overloading In this example, I overloaded two property accesses, in order to trigger in turn the __set() and __get() methods. I deliberately kept the code simple and readable, so you can see clearly how both methods are called in the scripts context, in addition to examining what type of output is generated by each of them. All right, after executing the previous code snippet, the following messages are displayed sequentially: Assigning value 'This is element 2' to element with Even when the example you saw before is rather trivial, it does demonstrate how two specific property accesses can be overloaded, resulting in the execution of the __set() and __get() methods respectively. Do you think were done now? Not yet. I want to conclude this article showing you a simple example of how to overload a method call in PHP 5, which can be useful for triggering custom code wrapped into a __call() method. Therefore, go ahead and read the next few lines to see how this will be achieved.
blog comments powered by Disqus |