How can I use PHP 5.3 Closures like We use Blocks in Ruby.
I never used ‘for’ Loop in Ruby due to using Blocks with ‘each’ ‘find_all’ ‘inject’ Methods.
How can I use PHP 5.3 Closures like Ruby Blocks and say bye-bye to ‘for’ Loops 🙂
Like Between { and } is a Closure(or Block or Anonymous Function)
fruit = %w[apple banana orange]
fruit.each { |f| print "#{f}, " }
I do it in PHP this way,
$fruit = array('apple', 'banana', 'orange');
foreach ($fruit as $f)
{
print "$f, ";
}
Is there a way to do this the Ruby way using PHP Closures as PHP 5.3 supports it.
If you are looking at using lambdas to iterate over a PHP array, there are certain functions that you could use to accomplish that. Better illustrate it, I used a wrapper class
enum:That being said, closures in PHP aren’t meant to replace the for loop nor do they behave like ruby blocks.