Bug #68596 [Opn->Csd]: Included file on a register_shutdown_function() does not define it's functions
| From: | nikic@php.net | Date: | Sat, 11 Mar 2017 18:09:31 +0000 |
| Subject: | Bug #68596 [Opn->Csd]: Included file on a register_shutdown_function() does not define it's functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-207790@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68596&edit=1
ID: 68596
Updated by: nikic@php.net
Reported by: php at maisqi dot com
Summary: Included file on a register_shutdown_function() does
not define it's functions
-Status: Open
+Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: Windows / Linux
PHP Version: 5.6.3
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This has been fixed in PHP 7: https://3v4l.org/EsDVZ
Presumably the problem was some namespace state leaking between nested compiles.
Previous Comments:
------------------------------------------------------------------------
[2014-12-12 16:58:26] php at maisqi dot com
Description:
------------
This error only manifests in a complex scenario:
1. A function registered with register_shutdown_function(), includes another file that defines
some functions;
2. A third file is required by the first and defines two classes with the same name, which throws
a fatal error;
3. This last file has a namespace and at least one of the classes (that share the same name)
inherits from a base class;
4. The shutdown function runs after the fatal error has occurred and includes the second file, but
it's functions never make it into the global scope. That problem does not happen with classes,
for example.
Note that if the third file does not have a namespace or if none of the same named classes inherit,
all goes as expected: a fatal error is thrown and the shutdown function has access to the functions
defined on the file it includes.
I included PHP examples for the scenario.
Test script:
---------------
<?php
/**
* driver.php
* ----------
* Registers a shutdown-function, that includes "error_handler.php" (which merely defines
* a innocuous function named "handle_error", and includes
"throw_fatal_error.php"
* (that provokes an error declaring two classes with the same name).
*
* What happens is that the fatal error is thrown, shutdownFunc() is called and
* "error_handler.php" is included -- but it occurs another fatal error, complaining that
* handle_error() (defined in "error_handler.php") is undefined.
* The problem does not happen with classes, as can be demonstrated by ErrorClass,
* which is defined in "error_handler.php" and it's existence checked on
shutdownFunc().
*
* VERY IMPORTANT NOTE
* If "throw_fatal_error.php" does not include a namespace or if neither class inherits
* from another, everything works ok!
*/
function shutdownFunc () {
require_once __DIR__ . '/error_handler.php';
echo "We're about to call <em>handle_error()</em>...<br />\n";
echo 'ErrorClass is ', class_exists ('ErrorClass') ? 'undefined' :
'defined', "<br />\n";
handle_error();
}
register_shutdown_function ('shutdownFunc');
// Make sure errors are printed.
ini_set ('display_errors', 'On');
// Include the file that provokes the error.
require 'throw_fatal_error.php';
<?php
/**
* error_handler.php
* -----------------
* Defines a function that simply prints a message.
*/
function handle_error () {
echo "handle_error() is here<br />\n";
}
class ErrorClass extends \stdclass {}
<?php
/**
* throw_fatal_error.php
* ---------------------
* Declares two classes with the same name, which throws a fatal error.
*/
namespace test;
class aclass {}
class aclass extends \stdclass {}
Expected result:
----------------
A «Fatal error: Cannot redeclare class aclass» should be thrown» but the following message
should also be printed:
«handle_error() is here»
Actual result:
--------------
No «handle_error() is here» message is printed the following is printed instead:
«Fatal error: Call to undefined function handle_error()»
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68596&edit=1