views:

61

answers:

1

i have to create a variable that is callable with php's is_callable

i have done this:

$callable = array(new MyClass, 'methodName');

but i want to pass a parameter to the method.

how can i do that?

cause using symfony's event dispatcher component will be like:

$sfEventDispatcher->connect('log.write', array(new IC_Log('logfile.txt'), 'write'));

the first parameter is just a event name, the second is the callable variable.

but i can only call the write method, i want to pass a parameter to it.

could someone help me out.

thanks

+1  A: 

You don't pass parameters to your listener callbacks (without extending the core). Symfony will be the one calling it and will pass an event object. If you need additional info, you can create a different method that calls another method where you can control the parameters.

$callable1 = array(new MyWriter, 'write1');
$callable2 = array(new MyWriter, 'write2'); // or $callable2 = array($callable1[0], 'write2');

$sfEventDispatcher->connect('log.write', $callable1);
$sfEventDispatcher->connect('log.write', $callable2);

And your callback class methods can be something like:

class MyWriter
{
    public function write($event, $num)
    {
        // do something
    }

    public function write1($event)
    {
        $this->write($event, 1);
    }

    public function write2($event)
    {
        $this->write($event, 2);
    }

}

Alternatively, you can create properties that act as state that your write function can check:

class MyWriter
{
    public $state = 1;

    public function write($event)
    {
        if ($this->state == 1) {
            // do this
        } else {
            // do this instead
        }   
    }
}

This is trickier as you'd have to set state before a pertinent event is triggered which may not prove feasable, depending on the specifics of your situation:

$callable[0]->state = 2;
webbiedave