note 102445 modified in functions.anonymous by thiago
[email protected] Mon, 14 Feb 2011 19:57:41 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
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