Jörg
Jörg

Reputation: 127

Error Handling: set_error_handler() expects the argument (errorHandler) to be a valid callback

I've got a PHP form with several lines of code.

Now I want to log errors in the database.

I'm trying to do this by the following code:

<?php
set_error_handler("errorHandler");

//The following line produces an error for testing
echo $notexist;

function errorHandler($errno, $errstr, $errfile, $errline) {
    echo "error detected";
}
?>

Unfortunately PHP throws an error and I can't find out how to fix it:

"set_error_handler() expects the argument (errorHandler) to be a valid callback"

I think I defined the callback, didn't I?

Upvotes: 2

Views: 6045

Answers (1)

Rukky Kofi
Rukky Kofi

Reputation: 69

If the function is in a namespace you need to put the fully qualified name of the function (including the namespace).

For example:

 <?php

    namespace MyNamespace;

    set_error_handler("MyNamespace\t_errorHandler");

    echo $notexist;

    function t_errorHandler($errno, $errstr, $errfile, $errline) {
        echo "error detected";
    }
?>

Upvotes: 6

Related Questions