note 34845 modified in function.create-function by danbrown

From: Date: Sat, 19 Jul 2008 21:25:46 +0000
Subject: note 34845 modified in function.create-function by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141755@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: <?php $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 --was-- 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 http://php.net/manual/en/function.create-function.php

« previous php.notes (#141755) next »