How to pass arguments to callable method specifically for register_shutdown_function?

Oto Shavadze picture Oto Shavadze · Dec 4, 2012 · Viewed 8.1k times · Source

I have method, in this method may be happened fatal error, for catching this error I make this

class a {


    function shutDownFunction() { 
        $error = error_get_last();
        if ($error['type'] == 1) {
            echo "this is fatal error";
        } 
    }


    function terribleFunction () {
        register_shutdown_function(array($this,'shutdownFunction'));


        // here is code, wich may causes fatal error

    }


}

Okay, this understand, but I need pass argument from terribleFunction to shutDownFunction. How to make this?

Answer

Rawkode picture Rawkode · Dec 4, 2012

First you need to specify that shutDownFunction should accept a parameter.

function shutDownFunction($var)

Then you can call register_shutdown_function as so

register_shutdown_function(array($this, 'shutdownFunction'), $myVar);

Documentation is here and there are examples in the comments.