How to use a static class method as a filter for Kohana's Validation Library?
Kohana's Validation library 开发者_如何学Chas 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?
A callback is one of PHP's pseudo-types. It will let you pass a
- function
- method of an instantiated object
- 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.
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'));
精彩评论