When creating a simple function, it is sometimes appropriate to encapsulate a small section of logic in a sub-function. My question is:
Assuming we will never use the calc
function again, which of the following is easiest on the PHP parser when running this type of procedure?
1. A Nested Function: (PHP has to redefine calc
each time:)
function doSomething($a, $b, $c) {
$calc = function($val) { /* do some calculation */ };
if($a>$c) return $calc($c);
else if($a<$b) return $calc($b);
else return $calc($c);
}
2. A Second Function: (PHP has to keep calc
in global memory:)
function doSomething($a, $b, $c) {
if($a>$c) return calc($c);
else if($a<$b) return calc($b);
else return calc($c);
}
function calc($val) { /* do some calculation */ }
3. A Class: (More code, and still in global memory)
class something {
static public function doSomething($a, $b, $c) {
if($a>$c) return self::calc($c);
else if($a<$b) return self::calc($b);
else return self::calc($c);
}
static private function calc($val) { /* do some calculation */ }
}