Defining short, private PHP nested functions (subfunctions) - best performance? best practice?

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) { /* do some calculation */ };
    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) { /* do some calculation */ }

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) { /* do some calculation */ }
}
+4
source share
1

: PHP

, . : PHP , .

, 2 , 1, 3 - , , calc() .

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 */ } }

+2

Source: https://habr.com/ru/post/1655983/


All Articles