Page 1 of 1

Weird question but nice idea

Posted: Fri Oct 07, 2005 12:08 pm
by Chris Corbyn
Hey,

Anybody know if I can capture the name of a requested (invalid) method and do something with it before php throws an error?

I'm playing around with some funky dynamic variable/function stuff and this could be very useful to me.

Example:

Code: Select all

class foo
{
    private $x = 5;
    private $y = 13;
    
    public function x($z)
    {
        $this->{__FUNCTION__} = $z; //Cool 
    }

    public function showx()
    {
        echo $this->x;
    }
}

$foo = new foo();

$foo->showx(); //5
$foo->x(29);
$foo->showx(); //29

$foo->y(16); //PHP gives an error (rightfully so)
This is more a proof of concept but that last $foo->y(); I'd like to capture the fact that they requested a method called "y" and do something with it.... My guess is that this is impossible :(

Posted: Fri Oct 07, 2005 12:10 pm
by Chris Corbyn
PHP 5 by the way :)

Posted: Fri Oct 07, 2005 1:13 pm
by feyd
__call() the overloading magic method :)

Posted: Sat Oct 08, 2005 7:39 am
by Chris Corbyn
Awesome thanks... you're genius. That's even better than I was hoping for ... cuts about 1000 lines of code from something I'm doing :)