note 102445 modified in functions.anonymous by thiago

From: Date: Tue, 15 Feb 2011 03:57:41 +0000
Subject: note 102445 modified in functions.anonymous by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176999@lists.php.net to get a copy of this message
These Closures are really interesting to me .This would be helpful. <?php Class Operations{ public function ops($x,$y,$op){ switch ($op){ case 'ADD':return function() use($x,$y){ return $x+$y."<br>"; }; break; case 'SUB': return function() use ($x,$y){ return $x-$y."<br>"; }; break; default: return function(){ return "Operation is not supported by class"."<br>"; }; } } } $op = new Operations(); $fn1 = $op->ops(6,7,'ADD'); echo $fn1(); $fn2 = $op->ops(6,2,'SUB'); echo $fn2(); $fn2 = $op->ops(6,7,'MUL'); echo $fn2(); ?> --was-- These Closures are really interesting to me .This would be helpful. Class Operations{ public function ops($x,$y,$op){ switch ($op){ case 'ADD':return function() use($x,$y){ return $x+$y."<br>"; }; break; case 'SUB': return function() use ($x,$y){ return $x-$y."<br>"; }; break; default: return function(){ return "Operation is not supported by class"."<br>"; }; } } } $op = new Operations(); $fn1 = $op->ops(6,7,'ADD'); echo $fn1(); $fn2 = $op->ops(6,2,'SUB'); echo $fn2(); $fn2 = $op->ops(6,7,'MUL'); echo $fn2(); http://php.net/manual/en/functions.anonymous.php

« previous php.notes (#176999) next »