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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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/>";
Last edited by Christopher on Tue Aug 14, 2007 4:53 am, edited 1 time in total.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I wasn't able to find that in the manual.. is it not documented?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

(#10850)
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

CakePHP uses that. The prototype is "function __call($method, $params, &$return)" IIRC.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply