1

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?

Nick
  • 1,441
  • 11
  • 22
  • 1
    http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-us look at this... and ask if you have further questions – DerWaldschrat Aug 08 '12 at 12:20
  • Ah thanks, so i have to use function( $value ) use ( $format ){ } Funny PHP, but thanks, it works! – Nick Aug 08 '12 at 12:29

0 Answers0