Page 1 of 1

__call(), __get() and __set() in PHP4

Posted: Mon Aug 13, 2007 8:35 pm
by Christopher
You have been keeping this secret from me you rapscallions!

I don't know how I could have missed the overload() function in PHP! It seems that since PHP 4.3.0 you have been able to have __call(), __get() and __set() in PHP4. Well, it is a little late now to be to useful ... but it could have been -- even though the syntax is slightly different.

Try the following code in PHP4:

Code: Select all

<?php
error_reporting(E_NONE);

class Test {
    var $_data = array();

    function __call($method, $args, &$return) {
        echo  "__call: $method(" . implode (',', $args) . ')<br/>';
    }

    function __get($prop_name, &$prop_value) {
        $prop_value = isset($this->_data[$prop_name]) ? $this->_data[$prop_name] : null;
        echo  "__get: $prop_name, $prop_value<br/>";
    }

    function __set($prop_name, $prop_value) {
        $this->_data[$prop_name] = $prop_value;
        echo  "__set: $prop_name, $prop_value<br/>";
    }
}

overload ('Test');

$test =& new Test();
$test->foo();
$test->bar = 'foo';
$test = $test->bar;
echo "test->bar = $test<br/>";

Posted: Mon Aug 13, 2007 8:57 pm
by Benjamin
I wasn't able to find that in the manual.. is it not documented?

Posted: Mon Aug 13, 2007 8:58 pm
by Christopher

Posted: Tue Aug 14, 2007 3:46 am
by stereofrog
CakePHP uses that. The prototype is "function __call($method, $params, &$return)" IIRC.

Posted: Tue Aug 14, 2007 6:25 am
by Chris Corbyn
I vaguely remember seeing it mentioned but for some reason it never stuck in my head. Could be useful at times.

Oddly enough, my E_STRICT standards checking has stopped working when the "public" keyword is missing in my colleague's code.

Posted: Tue Aug 14, 2007 12:07 pm
by Christopher
d11wtq wrote:Oddly enough, my E_STRICT standards checking has stopped working when the "public" keyword is missing in my colleague's code.
I recall that in recent versions of PHP5 they have relaxed the checking to allow PHP4 style bare function and var declarations to default to public.