phpclosureskcachegrind

What is closure in KCacheGrind?


what is the meaning of closure in KCacheGrind? I have it with one of my functions and it is pointing out the spl_autoload_register() function, spl_autoload_call in KCacheGrind. And the self time of the function is 60+ so, of course, I want to optimize it, but I do not know where to start.

What is the closure in KCacheGrind?

What do I need to optimize the said function to lessen the self time?


Solution

  • A closure is a function that uses variables that are outside its local scope, but not global.

    I'll use a language agnostic example since it's been forever since I've written PHP:

    function someFunc() {
        var a = 0;
    
        return function() { // This is the closure
            a++;
            return a;
        } 
    } 
    
    var f = someFunc();
    
    print(f()); // Prints 1
    print(f()); // Prints 2
    print(f()); // Prints 3
    

    Note the first comment. That function being returned is a closure over the a variable.