When creating a simple function, it is sometimes advisable to encapsulate a small part of the logic into a subfunction. My question is:
Assuming we will never use a function again calc, which of the following is the simplest PHP parser when executing this type of procedure?
1. Nested function: (PHP must override calcevery time :)
function doSomething($a, $b, $c) {
$calc = function($val) { };
if($a>$c) return $calc($c);
else if($a<$b) return $calc($b);
else return $calc($c);
}
2. Second function: (PHP should store calcin 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) { }
3. 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) { }
}
source
share