Object Overloading in PHP 5


A fine implementation of the object-overloading paradigm has found its way into PHP version 5. This article explores the possibilities of the overload methods __call(), __set(), and __get(). After explaining the basic theory of overloading, it dives straight into the topic by using two practical examples: first, implementing persistable classes, and second, figuring out a way to realize dynamic getter and setter methods. If you do not yet know what these terms mean, don't be afraid--it will become clear to you when you see the example code.

Requirements
Besides being comfortable with PHP 5, you should understand the most basic object-oriented programming terms, such as class, property, method, and constructor. You should also know the semantics of the access modifiers private, protected, and public. If you feel like you don't meet these requirements, you can take a look at numerous introductions to object-oriented programming. For general knowledge, Sun's Object-Oriented Programming Concepts is worth a read. If you're looking for information about PHP's object-oriented syntax, the PHP Manual contains valuable information in Chapter 19, Classes and Objects (PHP 5).


What Is Object Overloading?
What is this all about? When talking about object overloading in PHP, people distinguish between two types:
o Method overloading
o Property overloading
In the case of method overloading, the code defines a magic class method, __call(), that will act as a wildcard for calls to undefined methods of the corresponding class. This wildcard method will be called only when the class does not contain the method you are trying to access. Without method overloading, the following example will cause PHP to display the error message Fatal error: Call to undefined method ThisWillFail::bar() in/some/directory/example.php on line 9 and abort the program execution:
<?php
class ThisWillFail {
public function foo() {
return "Hello World!";
}
}
$class = new ThisWillFail;
$class->bar();
?>


With the help of method overloading, code can catch such calls and handle them gracefully.
Property overloading works similarly to method overloading. In this case, the class redirects (or even delegates, as some OO buffs might call it) calls to read/write accesses to class properties that do not have explicit definitions in the class body. The special methods here are __set() and __get(). Depending on the error-reporting level (for more information, see David Sklar's excellent ONLamp article PHP Debugging Basics), the PHP interpreter will usually either issue a notice when accessing such an undefined property, or belatedly and silently define the variable. If using property overloading, the interpreter will instead call __set() when setting the value of an undefined property or __get() when accessing the value of such a property.

All things considered, overloading allows for drastically reduced development time in dynamic languages such as PHP.

So much for the theory and the buzzwords. It's time to study some code!

No comments

Enter your email address:

Delivered by FeedBurner

OR

 Subscribe in a reader

 
 
 
Tips for optimizing php code by PHP Expert Important SERVER Variables in PHP - By PHP Expert Improved Error Messages in PHP 5 - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert Object Overloading in PHP 5 Persistable Classes - By PHP Expert Dynamic Getter/Setter Methods - By PHP Expert New Functions in PHP 5 New Directives - By PHP Expert Exception Handling - By PHP Expert Password Encryption in PHP - By PHP Expert Output Buffering in PHP - By PHP Expert Page Excerpts Using CURL - By PHP Expert Quick and Easy Google Site Search - By PHP Expert Always Be Notified When Google Crawls Your Site - By PHP Expert How to POST Form Data using CURL - By PHP Expert Cryptography for web developers - By PHP Expert Dynamically Loading JavaScript Files - by PHP Expert What is Web 2.0 - By PHP Expert