I've got a class which constructs a set of predefined closures. So far so good, but now i want to create closures according to specific parameters giving to the creating-functions.
For example a dateformatter. The caller can specify a format (as parameter) which should be used inside the closure. I've tried making the $format a global inside the returned closure, but unfortunately that didn't help..
Since code is 100x clearer then just words, here's an example.
class ClosureCreate {
public static function getDateFormatter( $format ){
return function( $value ){
return date( $format, strtotime( $value ) );
};
}
}
I want the getDateFormatter to return a closure which formats the given $value (given to the closure) according to the $format specified in $format (getDateFormatter's parameter)..
Is this possible in PHP, or am I just asking to much of it?