Loosening dependencies with closures in PHP
2011-09-27
Today I ran into a little issue: how to pass generic logger object to method? I wanted to get some verbose output from method, which I call from Command, but onc time it should log with Symfony2 OutputInterface and other time it should use monolog logger. Of course I can make some wrapper class for both of them, but it would be kind of an overkill. The Closure from PHP 5.3 came with solution. In calling method I created closure like this:
On the second hand I’ve got:
As you can see I don’t need to care about logging – just call the closure with message and it will log it properly, depending on context.