lambda inside of lambda?
am 17.08.2007 06:26:48 von Chad Yoshikawa
I'm trying to use create_function to create a new function with a
function body which calls a lambda function, but with no luck.
I get an "unexpected $end" parser error. Is this possible, or do I
need to use some other approach?
Here is sample code which generates the error. The first part works,
the second part does not:
===============cut here======================================
function test($a,$b) {
if ($a==$b) return 0; return ($a<$b)?-1:1;
}
function outer($a,$b,$innerfunctionname) {
return $innerfunctionname($a,$b);
}
$innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
($a<$b)?-1:1;');
########First Part#############
#This works - prints -1
$outerfunction1 = create_function('$a,$b', "return outer(\$a,\
$b,'test');");
$result1 = $outerfunction1(3,4);
printf("Result1 is %d\n",$result1);
#######Second Part############
#This doesn't work -- prints "PHP Parse error: syntax error,
unexpected $end in /tmp/test.php(22) : runtime-created function on
line 1"
$outerfunction2 = create_function('$a,$b', "return outer(\$a,\
$b,'$innerfunction');");
$result2 = $outerfunction2(3,4);
printf("Result2 is %d\n",$result2);
?>
===============cut here======================================
Re: lambda inside of lambda?
am 17.08.2007 08:35:25 von gosha bine
Chad Yoshikawa wrote:
> I'm trying to use create_function to create a new function with a
> function body which calls a lambda function, but with no luck.
>
> I get an "unexpected $end" parser error. Is this possible, or do I
> need to use some other approach?
>
> Here is sample code which generates the error. The first part works,
> the second part does not:
> ===============cut here======================================
>
> function test($a,$b) {
> if ($a==$b) return 0; return ($a<$b)?-1:1;
> }
>
> function outer($a,$b,$innerfunctionname) {
> return $innerfunctionname($a,$b);
> }
>
> $innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
> ($a<$b)?-1:1;');
>
> ########First Part#############
> #This works - prints -1
> $outerfunction1 = create_function('$a,$b', "return outer(\$a,\
> $b,'test');");
> $result1 = $outerfunction1(3,4);
> printf("Result1 is %d\n",$result1);
>
> #######Second Part############
> #This doesn't work -- prints "PHP Parse error: syntax error,
> unexpected $end in /tmp/test.php(22) : runtime-created function on
> line 1"
> $outerfunction2 = create_function('$a,$b', "return outer(\$a,\
> $b,'$innerfunction');");
> $result2 = $outerfunction2(3,4);
> printf("Result2 is %d\n",$result2);
> ?>
>
> ===============cut here======================================
>
Looks like a php bug to me. Lambdas' names start with the nul symbol
(\x00) and create_function seems to handle that incorrectly.
A test case
$a = "foo \x00";
var_dump($a);
var_dump(eval("return '$a';"));
$f = create_function('', "return '$a';");
I would report this.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Re: lambda inside of lambda?
am 19.08.2007 14:46:24 von colin.mckinnon
On 17 Aug, 07:35, gosha bine wrote:
> Chad Yoshikawa wrote:
> > I'm trying to use create_function to create a new function with a
> > function body which calls a lambda function, but with no luck.
>
> > I get an "unexpected $end" parser error. Is this possible, or do I
> > need to use some other approach?
>
> > Here is sample code which generates the error. The first part works,
> > the second part does not:
> > ===============cut here======================================
> >
> > function test($a,$b) {
> > if ($a==$b) return 0; return ($a<$b)?-1:1;
> > }
>
> > function outer($a,$b,$innerfunctionname) {
> > return $innerfunctionname($a,$b);
> > }
>
> > $innerfunction = create_function('$a,$b','if ($a==$b) return 0; return
> > ($a<$b)?-1:1;');
>
> > ########First Part#############
> > #This works - prints -1
> > $outerfunction1 = create_function('$a,$b', "return outer(\$a,\
> > $b,'test');");
> > $result1 = $outerfunction1(3,4);
> > printf("Result1 is %d\n",$result1);
>
> > #######Second Part############
> > #This doesn't work -- prints "PHP Parse error: syntax error,
> > unexpected $end in /tmp/test.php(22) : runtime-created function on
> > line 1"
> > $outerfunction2 = create_function('$a,$b', "return outer(\$a,\
> > $b,'$innerfunction');");
> > $result2 = $outerfunction2(3,4);
> > printf("Result2 is %d\n",$result2);
> > ?>
>
> > ===============cut here======================================
>
> Looks like a php bug to me. Lambdas' names start with the nul symbol
> (\x00) and create_function seems to handle that incorrectly.
>
> A test case
>
> $a = "foo \x00";
> var_dump($a);
> var_dump(eval("return '$a';"));
> $f = create_function('', "return '$a';");
>
> I would report this.
>
> --
> gosha bine
>
> extended php parser ~http://code.google.com/p/pihipi
> blok ~http://www.tagarga.com/blok
Certainly I would expect it to work too.
I used create_function() quite a lot within PfP Studio and never had
this problem.
C.