November 17, 2009

11/17, @11:04am:

I’ve planned for a while the ability to call objects methods in dispatch_* functions. Useful if you want to group your controller functions in a class for example (or simply for those who can’t write a PHP project without having a class keyword in each file).

I’ve added Matěj Grabovský’s commit that adds support for dispatching to lambda functions/closures in the new 0.5 branch; and i forgot that it also “activates” the dispatching objects methods features!

Because we use call_user_func() to call our controller function, our parameter is a callback function, and can also be object methods, including static clas methods and lambda functions.

So now we can write

# will call my_hello_function() function
dispatch('/hello', 'my_hello_function');

# Static class method call, MyClass::hello();
dispatch('/hello', array('MyClass', 'hello'));

# Object method call, $obj->hello();
dispatch('/hello', array($obj, 'hello'));

# Static class method call (As of PHP 5.2.3), MyClass::hello();
dispatch('/hello', 'MyClass::hello');

# Using lambda function (As of PHP 5.3.0)
dispatch('/hello', function(){
  return 'Hello World!';
});

PS: thanks to datashaman for noticing it

blog comments powered by Disqus