Jump to content

The lambda_n Function and the create_function() Function


iwato

Recommended Posts

Challenge: Run this code twice and see if you do not obtain an error on the second run, but not on the first. Then turn off your test-server, and run it again. The same thing repeats itself. Sometimes, you have to run the code more than once before the error message kicks in.On the first run you should obtain the following series of numbers 2, 1, 3.

<?php	$f = create_function('', 'return 1;');	function lambda_1() { return 2; }	$g = 'lambda_1';	echo $g(), '<br />';	$h = chr(0) . 'lambda_1';	echo $h(), '<br />';?><hr /><?php	$i = create_function('', 'return 3;');	$j = chr(0) . 'lambda_2';	echo $j();?>

Roddy

Link to comment
Share on other sites

Yep... happens to me too. But I see the error only when PHP is running as an Apache module... Looks like by using a null character, you're successfully screwing up some kind of buffers PHP reuses across requests.Here's a minimal test case:

<?php	$f = create_function('', 'return 1;');	function lambda_1() { return 2; }	$g = 'lambda_1';	echo $g();	$h = chr(0) . 'lambda_1';	echo $h();?>

But before you report the PHP bug, you might also want to formulate the expected result... A null character should terminate a string, and that's what happens on subsequent runs.... I guess you could say "Always output 22, or always give an error at the second attempt."You are using PHP 5.3.3, right? Because the test which I did is on PHP 5.3.2... I'm just saying... the PHP dev team never review bugs submitted with a version lower than the latest public one.

Link to comment
Share on other sites

Yep... happens to me too.
Thank you for the confirmation
You are using PHP 5.3.3, right? Because the test which I did is on PHP 5.3.2... I'm just saying... the PHP dev team never review bugs submitted with a version lower than the latest public one.
No, I am using PHP 5.2.6, and I do not know whether MAMP-Pro will let me upgrade without a fee.Roddy
Link to comment
Share on other sites

What the heck are you doing, are you just trying to break PHP? Why would you have a null character start a function name? A null character is not even valid as the first character of a function name, so trying to execute that should throw a fatal error.You should be a professional software tester.

I do not know whether MAMP-Pro will let me upgrade without a fee
Do they actually charge for a bunch of free software?
Link to comment
Share on other sites

What the heck are you doing, are you just trying to break PHP?
Why would I want to break PHP? I am just trying to understand what a lambda function is, and what the create_function() actually creates.
Why would you have a null character start a function name? A null character is not even valid as the first character of a function name, so trying to execute that should throw a fatal error.
Well, not in this case, at least not with the first run. Do you understand that chr(0).lambda_1 and lambda_1 are two different functions, and that each returns a different value? Try it.
Do they actually charge for a bunch of free software?
Not exactly. They do charge for the interface of which PHP is a part, though.Roddy
Link to comment
Share on other sites

Yes they are 2 different functions but it is not valid to use a null character as the first character of a function name.From the PHP manual http://www.php.net/manual/en/functions.user-defined.php

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
You are not following the naming rules. I am surprised it runs even once.
Link to comment
Share on other sites

I am just trying to understand what a lambda function is, and what the create_function() actually creates.
It just creates a regular function. The only difference is that it has a random name. PHP doesn't really have lambda functions like you would find in something like Lisp (although PHP 5.3 does have anonymous functions), what you're doing there is just using a variable function, a variable that holds a function name. That's virtually exactly what create_function does, it returns a function name.What I don't understand is why you would possibly want to start a function name with a null character. It might have some academic value, but there's no practical purpose. You're specifically disobeying PHP's rules of naming functions, so it's no wonder that it doesn't work correctly.
Do you understand that chr(0).lambda_1 and lambda_1 are two different functions, and that each returns a different value?
Obviously, they have different names. That's not where my questions are, my questions involve why you would even do that in the first place. What is not clear is why prepending the function name with a null character causes PHP to execute an entirely different function, but I assume that has something to do with the symbol tables where PHP sees the null character and just stops at the first symbol, which happens to be the first function you defined. You may get different results if the first variable you defined was not a function.
Link to comment
Share on other sites

Has anyone ever used create_function in a serious implementation? I know what it does, but I can just barely imagine a situation where it might be useful. Any enlightenment? The online examples look like proof-of-concept toys.

Link to comment
Share on other sites

I've most often used it just for creating callbacks, like for array sorting. It would be unnecessary to use it in PHP 5.3 because of anonymous functions.
I did not know that you form anonymous function in PHP, so I am very happy that I posed this question -- still another incentive to upgrade from PHP 5.2.6.By the way, in so far it follows the lambda_n naming scheme, it seems wrong to say that the create_function() constitutes a randomly named function. Then too, we are still unclear as to how the chr() prefix distinguishes between the functions lambda_1 and chr().lambda_1.In any case, as Deirde's Dad and you have pointed out, it is unlikely relevant now that anonymous functions are available. Thanks to everyone.Roddy
Link to comment
Share on other sites

PHP 5.3 is all that widely deployed yet, but if you control your own server then it's perfect. Javascript has always had anonymous functions, and I find no end for uses of those.

By the way, in so far it follows the lambda_n naming scheme
That's right, I wasn't aware of that when I wrote that. I was looking into lambda functions in PHP and came across something that mentioned create_function, so I assumed that's why you were calling them lambda functions. That makes it sort of weird how this works:
	$f = create_function('', 'return 1;');	function lambda_1() { return 2; }	$g = 'lambda_1';

If create_function really does create a function named lambda_1, then the next line where you create another function with that name would ordinarily cause an error, you're not allowed to declare a function with the same name twice. But that's not what is going on, if you echo $f after calling create function you may be surprised if you refresh it several times. The undefined error you eventually get is actually the correct outcome, trying to run a function name that begins with a null character should produce that output. Try this:

	$func = chr(0) . 'mt_rand';	$val = $func();

This also doesn't work:

	$code = 'function ' . chr(0) . 'testfunc() {echo "test";}';	eval ($code);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...