php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80021 include/require documentation does not mention warnings for missing files
Submitted: 2020-08-26 17:45 UTC Modified: 2020-08-28 08:23 UTC
From: rsutman at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2020-08-26 17:45 UTC] rsutman at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.set-error-handler
---

Regarding:
"The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING independent of where they were raised, and most of E_STRICT raised in the file where set_error_handler() is called. "

`require` (https://www.php.net/manual/en/function.require.php) states "it will also produce a fatal E_COMPILE_ERROR level error."

But I'm using `set_error_handler` & my defined function IS handling a failed `require`. I found the solution on https://stackoverflow.com/a/14103883/802469

Test script:
---------------
https://stackoverflow.com/a/14103883/802469
Or my version (which should do it)

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

require("/not/a/file");

?>

Expected result:
----------------
I think the docs just need to be updated, unless I'm misunderstanding something. I very much like being able to catch fatal errors


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-26 20:03 UTC] requinix@php.net
-Summary: set_error_handler documentation +Summary: include/require documentation does not mention warnings for missing files -Status: Open +Status: Verified -Package: Documentation problem +Package: Scripting Engine problem
 [2020-08-26 20:03 UTC] requinix@php.net
Calling require() will give two messages:
1. A warning that /not/a/file does not exist
2. A fatal error because it could not open the file

Your error handler is catching #1.

Using include() would give:
1. A warning that /not/a/file does not exist
2. A warning that it could not open the file

The docs don't consider #1 because that's a regular warning coming from inside PHP when it goes looking for files. I think. require() and include() themselves are the ones responsible for #2 and that is what the docs talk about.

Those docs should probably point out that looking for a file that does not exist will raise a warning, and that it happens before the final fatal/warning is given.

Note that in PHP 8, require() will throw an Error exception instead of using a fatal error.
 [2020-08-28 08:23 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-08-28 08:24 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=4d9330093873ef89ca333776af2581f12ecda2c7
Log: Fix #80021: include/require documentation does not mention warnings for missing files
 [2020-08-28 10:05 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=224615dc6a7f32dd89498bfd630bcb748a062785
Log: Fix #80021: include/require documentation does not mention warnings for missing files
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Dec 04 18:01:23 2020 UTC