note 38977 added to function.create-function
| From: | caesarATdagoDOTde at rn2 dot php dot net | Date: | Tue, 13 Jan 2004 22:31:40 +0000 |
| Subject: | note 38977 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63478@lists.php.net to get a copy of this message | ||
The usage of dynamic created functions may be great, but the sample below will get trouble, if there
are any blocks ({...}) in the function. I tried to get a way to do this with regex, but i
didn't find any suitable. So I'm using the sample below to generate my user functions in a
daemonized environment and it works fine.
There may be the a problem with '{' and '}' in text because the script is
line-oriented, but maybe anybody else will get a solution for this. I thougt of the use of the
Tokenizer, but there is no token for the curls.
The php_check_syntax() function is new to php5 beta 3 so I recreated it for using it on versions
below php5, I think it's working fine so long.
<?php
if (!function_exists('php_check_syntax')) {
function php_check_syntax($file) {
$tmp = exec('php -l '.$file);
if (preg_match('/No/', $tmp))
return true;
else
return false;
}
}
$file = 'functions.inc.php';
$in_function = false;
$open = 0;
$close = 0;
$tmp = array(
'name' => '',
'args' => '',
'code' => ''
);
if (php_check_syntax($file))
$file = file($file);
else
die ("ERROR(s) in ".$file.".\n");
for ($i = 0; $i < count($file); $i++) {
if (preg_match('/function\ ?([a-zA-Z0-9-_]*)\ ?\((.*?)\)\ ?\{/ix', $file[$i],
$matches)) {
$in_function = true;
$open = substr_count($file[$i], '{');
$close = substr_count($file[$i], '}');
$tmp['name'] = $matches[1];
$tmp['args'] = $matches[2];
$tmp['code'] = '{';
} elseif ($open == $close && $open > 0) {
$funcs[] = $tmp;
$in_function = false;
} else {
$tmp['code'] .= $file[$i]."\n";
$open += substr_count($file[$i], '{');
$close += substr_count($file[$i], '}');
}
}
foreach ($funcs as $val)
$$val['name'] = create_function($val['args'], $val['code']);
?>
So long,
greetZ caesar alias n0pr0b
----
Manual Page -- http://www.php.net/manual/en/function.create-function.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38977
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38977&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38977&report=yes
Search -- http://master.php.net/manage/user-notes.php