the following code causes PHP to continually eat more and more memory where it should be destroying the old reference for $func.
while(1) {
$array = array();
$func = create_function('', "return 0;");
$array[] = $func;
$func = 0;
echo "sleeping for a second.\n";
sleep(1);
}

Patches

Pull Requests

History

This isn't a bug but how it works. Each create_function() call creates a new unique function. So obviously if you define zillions in an infinite loop you'll run out of memory.

[2003-12-26 18:27 UTC] josh at mykoala dot com

It's been said that this is just how the function works,
but it seems as though destroying allocations for an
anonymous function when redefining to the same variable
would be the way to handle it. Example:
for ($i=0; $i<10; $i++) {
$echoit = create_function('$wha', 'echo $wha;');
echo memory_get_usage();
$echoit(" on iteration: #$i\n");
}
How else can you dynamically modify function or code
without reloading a script?

[2004-02-23 19:46 UTC] jon at fuck dot org

create_function() is operating as it should, yes, but there is still no way to delete a function during the execution of a script. for silly things like irc bots it gets handy to be able to rehash your function set on the fly, which is possible, but you lose track of the functions you have created thus far when you redefine your array of hashes.
i suggest a delete_function($foo) that removes the created function from memory and renders the hash in $foo useless.
another useful one would be redefine_function(&$foo, create_function(...)) that shortcuts the delete/create step for us.
this comes in handy for silly things like irc bots, when you have a large set of functions to be rehashed without losing your connection to the server by terminating the script.