note 34845 added to function.create-function
| From: | nospam at fiderallalla dot de | Date: | Fri, 08 Aug 2003 11:39:58 +0000 |
| Subject: | note 34845 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-53638@lists.php.net to get a copy of this message | ||
Sometimes it may be useful to create functions in a dynamic environment
(f. e. in a daemon-like php script).
Normally declaring a function must be done once, which results in the problem,
that in this special case modifying a function wouldn't have an effect until the script is
reloaded.
Maybe this code snipplet is useful 4 u.
File: "functions.inc"
<?php
function test($str) {
echo $str;
}
?>
Dynamic FunctionHandler:
<?
$FileName = "functions.inc";
$FileHandle = fopen($FileName,"r");
$FileContent = fread($FileHandle,filesize($FileName));
fclose($FileHandle);
preg_match_all("#function\ ?([a-zA-Z0-9-_]*)\ ?\((.*?)\)\
?\{(.*?)\}#mixse",$FileContent,$Matches);
if ( is_array($Matches) && isset($Matches[0]) && count($Matches[0]) > 0 ) {
foreach ( $Matches[0] as $key=>$val ) {
$$Matches[1][$key] = create_function($Matches[2][$key],$Matches[3][$key]);
}
}
?>
The Test:
<?php echo $test("test"); ?>
.. will echo "test";
Hans Kuhlen
----
Manual Page -- http://www.php.net/manual/en/function.create-function.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34845
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34845&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34845&report=yes