note 103080 added to function.create-function
| From: | DaveH at osu1 dot php dot net | Date: | Thu, 24 Mar 2011 14:47:01 +0000 |
| Subject: | note 103080 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178163@lists.php.net to get a copy of this message | ||
The following function is very useful for creating an alias of a user function.
For built-in functions, it is less useful because default values are not available, so function
aliases for built-in functions must have all parameters supplied, whether optional or not.
function create_function_alias($function_name, $alias_name)
{
if(function_exists($alias_name))
return false;
$rf = new ReflectionFunction($function_name);
$fproto = $alias_name.'(';
$fcall = $function_name.'(';
$need_comma = false;
foreach($rf->getParameters() as $param)
{
if($need_comma)
{
$fproto .= ',';
$fcall .= ',';
}
$fproto .= '$'.$param->getName();
$fcall .= '$'.$param->getName();
if($param->isOptional() && $param->isDefaultValueAvailable())
{
$val = $param->getDefaultValue();
if(is_string($val))
$val = "'$val'";
$fproto .= ' = '.$val;
}
$need_comma = true;
}
$fproto .= ')';
$fcall .= ')';
$f = "function $fproto".PHP_EOL;
$f .= '{return '.$fcall.';}';
eval($f);
return true;
}
----
Server IP: 64.71.164.2
Probable Submitter: 173.9.30.225
----
Manual Page -- http://www.php.net/manual/en/function.create-function.php
Edit -- https://master.php.net/note/edit/103080
Del: integrated -- https://master.php.net/note/delete/103080/integrated
Del: useless -- https://master.php.net/note/delete/103080/useless
Del: bad code -- https://master.php.net/note/delete/103080/bad+code
Del: spam -- https://master.php.net/note/delete/103080/spam
Del: non-english -- https://master.php.net/note/delete/103080/non-english
Del: in docs -- https://master.php.net/note/delete/103080/in+docs
Del: other reasons-- https://master.php.net/note/delete/103080
Reject -- https://master.php.net/note/reject/103080
Search -- https://master.php.net/manage/user-notes.php