{
    "slug": "php_error_handler",
    "term": "Custom Error Handlers (set_error_handler)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "set_error_handler() lets you replace PHP's default error handling with a custom callback — essential for logging, alerting, and graceful degradation.",
    "long": "PHP triggers errors via a global error handler. set_error_handler(callable $handler) replaces it for user-level errors (E_WARNING, E_NOTICE, E_USER_ERROR etc.) but cannot catch E_ERROR, E_PARSE, or E_CORE_ERROR — those still kill the script unless register_shutdown_function() is used. The callback receives ($errno, $errstr, $errfile, $errline). Always restore the previous handler with restore_error_handler() in tests. set_exception_handler() is the equivalent for uncaught exceptions.",
    "aliases": [],
    "tags": [
        "php",
        "error-handling",
        "logging"
    ],
    "misconception": "set_error_handler() catches all errors — it cannot catch fatal errors (E_ERROR, E_PARSE). Use register_shutdown_function() + error_get_last() to handle those.",
    "why_it_matters": "Custom error handlers let you log errors to external systems, convert them to exceptions, and prevent sensitive paths from leaking in error messages.",
    "common_mistakes": [
        "Forgetting to return true from the handler — PHP then executes the internal handler too.",
        "Not restoring the previous handler — breaks test isolation.",
        "Using set_error_handler() alone without register_shutdown_function() — misses fatal errors."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_exception_hierarchy",
        "error_handling",
        "php_error_levels"
    ],
    "prerequisites": [
        "php_error_levels",
        "exception_handling"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.set-error-handler.php"
    ],
    "bad_code": "set_error_handler(function($errno, $errstr) {\n    // Silently ignores — errors disappear\n});",
    "good_code": "set_error_handler(function(int $errno, string $errstr, string $errfile, int $errline): bool {\n    throw new \\ErrorException($errstr, 0, $errno, $errfile, $errline);\n});\n\nregister_shutdown_function(function() {\n    $error = error_get_last();\n    if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR])) {\n        // Log fatal error before script dies\n        error_log(\"Fatal: {$error['message']} in {$error['file']}:{$error['line']}\");\n    }\n});",
    "quick_fix": "Register handler with set_error_handler(fn($errno,$errstr,$errfile,$errline) => throw new ErrorException($errstr,0,$errno,$errfile,$errline)) — then catch ErrorException everywhere.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_error_handler",
        "html_url": "https://codeclaritylab.com/glossary/php_error_handler",
        "json_url": "https://codeclaritylab.com/glossary/php_error_handler.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Custom Error Handlers (set_error_handler)](https://codeclaritylab.com/glossary/php_error_handler) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_error_handler"
            }
        }
    }
}