The last practical example that I’ll show you with reference to overloading classes in PHP 5 is completely focused on calling the “__call()” method automatically, when a method call is correctly overloaded. Maybe this sounds confusing, so first I’ll redefine the prior “DataSaver” class to provide a concrete definition for the “__call()” method that I mentioned before. Here’s the source code for this class: class DataSaver{ As shown above, the “DataSaver” class has an additional “__call()” method, which will be automatically triggered if a method call is overloaded deliberately. Given that, here is a simple script that shows how to overload a method call, which obviously fires up the method in question: // example of method overloading with __call() method If you examine the above example in detail, the corresponding “__call()” method is triggered by the following line: $revData=$dataSaver->myMethod('Element A','Element B','Element As you can see, all that this line does is call the “myMethod()” method, in this way enforcing the triggering of “__call()”. Also, it should be noticed that the pertinent arguments passed when overloading a method call will be treated as an array, therefore the output produced by the previous script will be the following: Method myMethod has been called with the following arguments: The above listing clearly demonstrates that the “__call()” method has been triggered after overloading a method call, since the array of incoming arguments is first echoed normally, then reversed and finally displayed again, in accordance with the logic implemented by this method. At this point, I provided you with different practical examples of how to overload members and methods in PHP 5, which can be pretty useful if you want to run custom code defined within “__set()”, “__get()” and “__call()” methods. As I said before, certainly class overloading isn’t one of the strongest features of PHP, but with a little bit of willpower and the appropriate knowledge, you’ll get the most out of it. Wrapping up Over this three-part series, you hopefully learned the basics of class overloading in PHP 4/PHP 5. In all the cases I kept the code samples simple and readable, so you can understand more easily how they work. Although overloading objects in PHP seems to be a rather complex topic at first glance, this impression should disappear progressively, if you get more experience on the subject. As usual, see you in the next PHP tutorial!
blog comments powered by Disqus |