note 72430 added to function.create-function
| From: | adotsteenveldatiddotumcndotnl at osu1 dot php dot net | Date: | Thu, 18 Jan 2007 14:18:37 +0000 |
| Subject: | note 72430 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121885@lists.php.net to get a copy of this message | ||
It is possible to use this call to implement continuations but you need a small workaround for a
nagging feature of create_function(). The result of this function does start with a null character
which might result in loosing the name of your function altogether!. (See also bug report #40160)
Here is a bit of code to play with. The result should be 'f(2, 2) = 6'
<?php
/* continuations in php.
vim:nu
Code inspired by http://www.ps.uni-sb.de/~duchier/python/continuations.html
*/
function writeln($s) { echo "$s\n"; }
function lambda0 ($args, $code)
{ return substr(create_function ($args, $code), 1);
}
function L ($l)
{ if (strncmp($l, 'lambda_', 7) === 0) return "\0$l";
else return $l;
}
function mul ($x, $y, $c) { $f = L($c); $f($x*$y); }
function add ($x, $y, $c) { $f = L($c); $f($x+$y); }
function mal ($x, $y, $c) { mul(2, $x, lambda0 ('$v', "add(\$v, $y, $c);")); }
function f($x, $y)
{ mal($x, $y, lambda0 ('$v', "writeln(\"f($x, $y) = \$v\");"));
}
f(2, 2);
?>
----
Server IP: 83.137.20.120
Probable Submitter: 131.174.244.4
----
Manual Page -- http://www.php.net/manual/en/function.create-function.php
Edit -- https://master.php.net/note/edit/72430
Del: integrated -- https://master.php.net/note/delete/72430/integrated
Del: useless -- https://master.php.net/note/delete/72430/useless
Del: bad code -- https://master.php.net/note/delete/72430/bad+code
Del: spam -- https://master.php.net/note/delete/72430/spam
Del: non-english -- https://master.php.net/note/delete/72430/non-english
Del: in docs -- https://master.php.net/note/delete/72430/in+docs
Del: other reasons-- https://master.php.net/note/delete/72430
Reject -- https://master.php.net/note/reject/72430
Search -- https://master.php.net/manage/user-notes.php