php-src/ext/overload
Andrei Zmievski b197b8d104 This commit implements property-specific accessors.
When overload() is called, it not only scans for general handlers, but
also for any methods beginning with __get_ and __set_ and saves a list of
them. Then when a property is accessed, e.g. $obj->x, the extension will first
check whether __get_x() is available, and will call it if it is, otherwise
it will invoke the general __get() handler.
2001-12-11 04:41:15 +00:00
..
config.m4 Enable it for real. 2001-12-10 00:38:18 +00:00
CREDITS *** empty log message *** 2001-12-09 04:34:54 +00:00
EXPERIMENTAL Adding user-space object overloading extension. 2001-10-15 20:32:56 +00:00
Makefile.in Adding user-space object overloading extension. 2001-10-15 20:32:56 +00:00
overload.c This commit implements property-specific accessors. 2001-12-11 04:41:15 +00:00
overload.dsp fix CFLAGS, how could this have worked before? 2001-12-02 14:01:22 +00:00
php_overload.h This commit implements property-specific accessors. 2001-12-11 04:41:15 +00:00
README Modified __call() handler to accept a byref return value parameter that 2001-12-10 17:20:04 +00:00

This extension is experimental.

That's all I'm required to say, as you should know the consequences, but
I'll go ahead and add a few more notes.

The purpose of this extension is to allow user-space overloading of object
property access and method calls. It has only one function, overload() which
takes the name of the class that should have this functionality enabled. But
the class has to define appropriate methods if it wants to have this
functionality: __get(), __set(), and __call(). So, overloading can be
selective.

Inside each handler the overloading is disabled so you can access object
properties normally.


Usage
-----
<?php

class OO {
    var $a = 111;
    var $elem = array('b' => 9, 'c' => 42);

    function OO($aval = null)
    {
        $this->a = $aval;
    }

    function __get($prop_name, &$prop_value)
    {
        if (isset($this->elem[$prop_name])) {
            $prop_value = $this->elem[$prop_name];
            return true;
        } else
           return false;
    }

    function __set($prop_name, $prop_value)
    {
        $this->elem[$prop_name] = $prop_value;
        return true;
    }

    function __call($method, $args, &$return_value)
    {
        print '-- OO::' . $method . "() was called.--\n";
        $return_value = call_user_func_array(array(&$this, 'my_' . $method), $args);
        return true;
    }

    function my_whatever($f1, $f2, $f3)
    {
        var_dump($f1, $f2, $f3);
        return $f1 + $f2;
    }
}

overload('OO');

$o = new OO;
print "\$o->a: $o->a\n";
print "\$o->b: $o->b\n";
print "\$o->c: $o->c\n";
print "\$o->d: $o->d\n";

$val = new stdclass;
$val->prop = 555;

$o->a = array($val);
var_dump($o->a[0]->prop);

var_dump($o->whatever(1, 2, 'a'));

?>

What works
----------
Whatever you can get it to do.


What doesn't work
-----------------
Invoking original overloading handlers, if the class had any.
__set() only works to one level of property access, no chains yet
Whatever else I am forgetting about here.


What might change
-----------------
Hell, anything, even the name of extension and its only function.

Feedback, please.