php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #70929 create_function now throws ParseError
Submitted: 2015-11-17 18:39 UTC Modified: 2015-12-17 11:38 UTC
From: bishop@php.net Assigned: cmb (profile)
Status: Closed Package: Documentation problem
PHP Version: 7.0Git-2015-11-17 (Git) OS:
Private report: No CVE-ID: None
 [2015-11-17 18:39 UTC] bishop@php.net
Description:
------------
In PHP versions 5.3.19 - 5.6.15, a parse error in create_function returns false, per the manual:

> If there is a parse error in the evaluated code, eval() returns FALSE and 
> execution of the following code continues normally.

As of PHP 7, this outputs only a parse error and terminates the script.

See https://3v4l.org/apEOR#v5319

Test script:
---------------
<?php

$a = create_function('', 'return (return;);');
var_dump($a, 'here');

Expected result:
----------------
Parse error: syntax error, unexpected 'return' (T_RETURN) in %s : runtime-created function on line %d
bool(false)
string(4) "here"

Actual result:
--------------
Parse error: syntax error, unexpected 'return' (T_RETURN) in %s : runtime-created function on line %d

Patches

capture_parse_exceptions (last revision 2015-11-17 18:40 UTC by bishop@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-17 18:40 UTC] bishop@php.net
The following patch has been added/updated:

Patch Name: capture_parse_exceptions
Revision:   1447785604
URL:        https://bugs.php.net/patch-display.php?bug=70929&patch=capture_parse_exceptions&revision=1447785604
 [2015-11-17 18:54 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-11-17 18:54 UTC] nikic@php.net
You have to catch ParseError (or Error, or Throwable), not Exception: https://3v4l.org/4MOJt
 [2015-11-17 19:01 UTC] bishop@php.net
-Summary: create_function regression when parse error present +Summary: create_function now throws ParseError -Status: Not a bug +Status: Open -Type: Bug +Type: Documentation Problem -Package: Scripting Engine problem +Package: Documentation problem
 [2015-11-17 19:01 UTC] bishop@php.net
As of PHP 7, create_function now throws an exception when there's an internal parse error:

try {
   $result = create_function('', 'return (return 1);');
} catch (ParseError $ex) {
   $result = $ex->getMessage();
}

https://3v4l.org/oCHLh#v700alpha2

The docs should be updated to reflect this.

---

Thanks @nikic, was just about to say that. :)
 [2015-11-17 19:10 UTC] bishop@php.net
This is mentioned indirectly in the RFC, while discussing eval:
https://wiki.php.net/rfc/engine_exceptions_for_php7#e_parse_compatibility
 [2015-12-17 11:38 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=338282
Log: fixed #70929: create_function now throws ParseError
 [2015-12-17 11:38 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-12-17 11:38 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=a0a0165e07a83d74a66c316d52b04c1f4bf52aeb
Log: fixed #70929: create_function now throws ParseError
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC