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

 
jQuery UI provides a comprehen
 
Program Plan   I drafted a p
 
I present to you my skills, ac
 
Introduction One of the issue
 
If you are a PHP developer and
 
cURL is a great tool to help y
 
cformsII cforms is a powerful
 
  The lack of Unicode su
 
History PHP-GTK was origina
 
Performance on the web is stra
 
Listen t
 
What\'s the number one cost in
 
When you\'re discussing the In
 
Classe
 
A service-oriented architectur
 
Introduc
 
PHP Crons and Linux Linux has
 
Cross site scripting (XSS) is
 
What Makes a Web 2.0 Applicati
 
As you develop web application
 
Cryptogr
 
Posting
 
Have you
 
Resources The Google API - ht
 
Get Started
 
Output B
 
If you r
 
PHP has some really sweet new
 
There were some new php.ini di
 
In PHP 5 there are some new fu
 
The following code implements
 
The following code snippet imp
 
A fine implementation of the o
 
Exception handling PHP 5 adds
 
Support for dereferencing obje
 
Static members Classes defini
 
Explicit object cloning In or
 
final methods The final keywo
 
Interfaces Gives the ability
 
The new object oriented featur
 
Sometimes its the little thing
 
Consider your file is at locat
 
# If a method can be static, d