views:

162

answers:

2

Kohana's Validation library has a pre_filter() method which lets you apply any PHP function to fields to be validated, as trim(), etc.
I tried to use a static method as a filter, but won't work:
$validation->pre_filter( 'field_name', 'class::method' )

What that does is create two filters, one with class and antoher one with method.

Any clues?

+1  A: 

To use a static method call back the callback needs to be an array.. for example:

array('MyCoolClassName', 'methodName');

so assuming its using call_user_func then your method call should be:

$validation->pre_filter( 'field_name', array('MyCoolClassName', 'methodName'));

or if you need to use an object instance:

$validation->pre_filter( 'field_name', array($objectInstance, 'methodName'));

prodigitalson
+2  A: 

A callback is one of PHP's pseudo-types. It will let you pass a

  1. function
  2. method of an instantiated object
  3. static method/class method

to a PHP function/method that's expecting a callback, and the PHP function/method will know what to do with it.

From the manual

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo(), empty(), eval(), exit(), isset(), list(), print() or unset().

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.

So, to use a static method in place of a callback string, you'd use

array('className','methodName');

If Kohana is using standard PHP callbacks, this should give you what you want.

Alan Storm
Thanks to both answers, they look like correct PHP, however I suspect Kohana's Validation lib is doing some naughty eval() or something, because it doesn't work with this. But thanks!
Petruza