|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-04-08 05:35 UTC] a at b dot c dot de
Description: ------------ When inserting (the name of) a lambda function provided by create_function into a string that will be used to create a second lambda function, the second function fails to parse. The parse error message makes what appears to be a spurious complaint about the code; only Test 5's error message makes sense (plus, it seems there is no "callable" version of a lambda function's name). (Cf. bug #10721 - parser chokes on lambda function names in eval'd code) Reproduce code: --------------- I made five distinct tests to bracket the issue: TEST 1: // Creating a function that calls an ordinary function (explict name) function always_true(){return true;} $afun = create_function('', 'return !always_true();'); echo "Calling $afun() " . ($afun() ? "returns true\n" : "returns false\n"); TEST 2: // Creating a function that calls an ordinary function (variable name) function always_true(){return true;} $b1 = 'always_true'; $bfun = create_function('','return !'.$b1.'();'); echo "Calling $bfun() " . ($bfun() ? "returns true\n" : "returns false\n"); TEST 3: // Creating a function that calls an anonymous function (explicit name); $c1 = create_function('','return true;'); $cfun = create_function('','return !'.chr(0).'lambda_3();'); echo "Calling $cfun() " . ($cfun() ? "returns true\n" : "returns false\n"); TEST 4: // Creating a function that calls an anonymous function (variable name) $d1 = create_function('','return true;'); $dfun = create_function('','return !'.$d1.'();'); echo "Calling $dfun() " . ($dfun() ? "returns true\n" : "returns false\n"); TEST 5: // Using is_callable to get a callable version of anonymous function name $e1 = create_function('', 'return true;'); if(!is_callable($e1,false,$callable_e1)) die("Couldn't call anonymous function."); echo "Using $callable_e1 as name of anonymous function\n"; $efun = create_function('','return !'.$callable_e1.'();'); echo "Calling $efun() " . ($efun() ? "returns true\n" : "returns false\n"); Expected result: ---------------- TEST 1: Calling lambda_1() returns false TEST 2: Calling lambda_1() returns false TEST 3: Calling lambda_2() returns false TEST 4: Calling lambda_2() returns false TEST 5: Using lambda_1 as name of anonymous function [?] Calling lambda_2() returns false Actual result: -------------- Actual Results: TEST 1: Calling lambda_1() returns false TEST 2: Calling lambda_1() returns false TEST 3: Parse error: parse error, unexpected '}' in test.php : runtime-created function on line 1 Fatal error: Function name must be a string in test.php on line 4 TEST 4: Parse error: parse error, unexpected '}' in test.php : runtime-created function on line 1 Fatal error: Function name must be a string in test.php on line 4 TEST 5: Using as name of anonymous function Parse error: parse error, unexpected ')' in test.php : runtime-created function on line 1 Fatal error: Function name must be a string in test.php on line 6 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 04:00:01 2025 UTC |
_Would it be sufficient if:_ instead of prefixing the lambda function with a NUL it were to be prefixed by '__'? Since PHP has (always?) claimed symbols starting with those characters for magical purposes anyone who is already declaring functions with names like "__lambda_1" is already effectively on notice that their code might break (moreso than someone who, in PHP 4, say, declared a function named "clone"). As it stands, higher-order functions are pretty much unworkable: attempts to construct new functions from existing ones are hamstrung by the fact that the existing functions may themselves be lambda functions and therefore unmentionable in the new function's body. Currently the only valid workaround would appear to be something like (to use the notation of the original examples) $dfun = create_function('','$d1=chr(0)."'.substr($d1,1).'"; return !$d1();'); Which of course needs to be further modified to deal with the cases where $d1 is not the name of a lambda function.