Closure Assignment Issue in Custom Error Handling

In the example found at >> https://akrabat.com/overriding-slim-3s-error-handling/
pasted below for easy access :
###==============================

Overriding Slim 3’s error handling ( Copy Pasted )

$container = $app->getContainer();
 
$container['errorHandler'] = function ($container) {
    return function ($request, $response, $exception) use ($container) {
        // retrieve logger from $container here and log the error
        $response->getBody()->rewind();
        return $response->withStatus(500)
                        ->withHeader('Content-Type', 'text/html')
                        ->write("Oops, something's gone wrong!");
    };
};
 
$container['phpErrorHandler'] = function ($container) {
    return function ($request, $response, $error) use ($container) {
        // retrieve logger from $container here and log the error
        $response->getBody()->rewind();
        return $response->withStatus(500)
                        ->withHeader('Content-Type', 'text/html')
                        ->write("Oops, something's gone wrong!");
    };
};

If you don’t want to repeat yourself, you can register the phpErrorHandler like this:

$container['phpErrorHandler'] = function ($container) {
    return $container['errorHandler'];
};

###==============================

I modified the If you don’t want to repeat yourself section in 5 different ways (pasted below), of which Style 4 and 5 , is creating problem. All other works fine.

Is it due to the getter and setter mechanism, implementation behaviour of $container[‘key’] within Slim ?

If not, I need more dig on PHP Basics !!!
Please guide.

Thanks in advance :slight_smile:
Susanth K

:slight_smile:

Style #1 ~~~~~~~~~~

$myErrorHandler = function ($container) {
//…
};

$container['phpErrorHandler'] = $myErrorHandler;
$container['errorHandler'] = $myErrorHandler;
// WORKS COOL :)

:slight_smile:

Style #2 ~~~~~~~~~~

$container['phpErrorHandler'] = $container['errorHandler'] = function ($container) {
//…
};
// WORKS COOL :)

:slight_smile:

Style #3 ~~~~~~~~~~

$container['errorHandler'] = $container['phpErrorHandler'] = function ($container) {
//…
};
// WORKS COOL :)

:bug:

Style #4 ~~~~~~~~~~

$container['phpErrorHandler'] = function ($container) {
//…
};

$container['errorHandler'] = $container['phpErrorHandler'];

// NOT WORKING :(
// Warning: Missing argument 2 for {closure}()
// Warning: Missing argument 3 for {closure}()

:bug:

Style #5 ~~~~~~~~~~

$container['errorHandler'] = function ($container) {
//…
};

$container['phpErrorHandler'] = $container['errorHandler'];

// NOT WORKING :(
// Warning: Missing argument 2 for {closure}()
// Warning: Missing argument 3 for {closure}()

The 4th and 5th are calling (initializing) phpErrorHandlerand errorHandler instead of referring to the closure factory. This is due to Pimple implementing ArrayAccess.

1 Like

@JoeBengalen Thank you so much.