This post is written for developers who use the PHP programming language. It provides a technique for catching a common class of typo by using the __call() __get() and __set() overloading methods.
One of the more powerful features of a scripting language like PHP is dynamic object extensibility. You want a new member in a class, just reference it and away you go.

Unfortunately when writing large production grade applications, this can be a problem: any misspelled member name goes by without raising an error. Good programming discipline means declaring everything you plan to use except in some very specific circumstances.

PHP5 provides “magic methods” that let you detect when undeclared members and methods are being referenced, so now it’s possible to trap these cases explicitly and raise an exception. This is a real time saver and it kills off a class of bug that can be very difficult to find.

Here’s my overload trapping class:

/**
 * This class traps attempts to access methods and
 * members that aren't in the class definition and to
 * throw an exception when this occurs.
 * @author Alan Langford
 * @license Public domain. Use as you wish.
 */
class OverloadCheckObject {
    static $overloadCheckObjectExceptionType;
    
    function __call($name, $args) {
        $ec = self::$overloadCheckObjectExceptionType;
        throw new $ec('Attempt to call undeclared method '
            . get_class($this) . '::' . $name);
    }
    
    function __get($name) {
        $ec = self::$overloadCheckObjectExceptionType;
        throw new $ec('Attempt to access undeclared member '
            . get_class($this) . '::' . $name);
    }
    
    function __set($name, $value) {
        $ec = self::$overloadCheckObjectExceptionType;
        throw new $ec('Attempt to assign undeclared member '
            . get_class($this) . '::' . $name);
    }
    
}
Mastodon