php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60364 Implement ability to recover from fatal errors in eval()'d code
Submitted: 2011-11-23 13:27 UTC Modified: 2015-06-13 12:33 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:3 (75.0%)
From: zyss at mail dot zp dot ua Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.8 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zyss at mail dot zp dot ua
New email:
PHP Version: OS:

 

 [2011-11-23 13:27 UTC] zyss at mail dot zp dot ua
Description:
------------
Sometimes eval() is used as a way to execute PHP code within a shell (legal remote access for debugging purposes) or to execute code compiled to PHP from higher-level scripting language or the code stored in the database etc.

The common problem is that a call of non-existing function (or object instantiation) results in termination of the whole script, not just eval'd code.

I think that it's not correct in such cases.

The obvious way to implement it would be adding flags to eval() function (as a second argument) one of which could indicate that eval() should not terminate the script but just return an error or raise an exception.

Such flags could be:
EVAL_FATAL_DIE        // current behavior
EVAL_FATAL_ERROR      // return FALSE
EVAL_FATAL_EXCEPTION  // raise an exception of a predefined class (e.g. EvalException)

Test script:
---------------
try {
  eval('non_existing_function()', EVAL_FATAL_EXCEPTION);
}
catch (EvalException $e) {
  Logger::log('Error in eval\'d code', $e);
}

Expected result:
----------------
Exception being logged and script continued its execution.

Actual result:
--------------
Fatal error:  Call to undefined function non_existing_function() in ... : eval()'d code(1) on line 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-20 03:46 UTC] hanskrentel at yahoo dot de
Why specific for eval and not include and/or overal?

This could be solved more straight forward solved by making fatal errors an Exception in PHP, like suggested in #28331 (which names eval in a comment as a use-case).
 [2011-12-20 15:05 UTC] zyss at mail dot zp dot ua
> This could be solved more straight forward solved by making fatal errors an Exception in PHP

Yes, that would be ideal (this is how Java handles missing classes and methods and it works just fine).

PHP borrowed from Java many features (classes, interfaces, "instanceof", ...), why not to implement one more useful thing — not to kill scripts when something is missing?
 [2015-06-12 17:50 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2015-06-12 17:50 UTC] cmb@php.net
Would that satisfy your request: <http://3v4l.org/6damM>? (Note
that the concrete Exception class is subject to change until PHP
7.0.0 GA.)
 [2015-06-12 18:03 UTC] zyss at mail dot zp dot ua
-Status: Feedback +Status: Assigned
 [2015-06-12 18:03 UTC] zyss at mail dot zp dot ua
php7@20150401 gives possibility to catch this type of errors with the help of EngineException.

Thanks.
 [2015-06-13 12:33 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2015-06-13 12:33 UTC] cmb@php.net
PHP 7.0.0 turns several fatal errors into exceptions[1], so it
will be possible to catch them. The concrete name of the
exceptions are not finally decided yet, however.[2]

[1] <https://wiki.php.net/rfc/engine_exceptions_for_php7>
[2] <https://wiki.php.net/rfc/throwable-interface>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC