note 74045 modified in function.func-get-args by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 21:40:14 +0000 |
| Subject: | note 74045 modified in function.func-get-args by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141762@lists.php.net to get a copy of this message | ||
Here is another variation on accepting a variable number of arguments. This allows for a variable
number of arguments to be passed to a Class constructor, as well as a customized class version to be
used dynamically. Syntax in code is:
<?php
$mail = Generator("MailClassName", $db_ref);
function Generator() {
$numargs = func_num_args();
$classname = func_get_arg(0);
$argstring='';
if ($numargs > 1) {
$arg_list = func_get_args();
for ($x=1; $x<$numargs; $x++) {
$argstring .= '$arg_list['.$x.']';
if ($x != $numargs-1) $argstring .= ',';
}
}
if (class_exists("Custom{$classname}")) {
$classname = "Custom{$classname}";
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
?>
Hope this is of use to someone.
--was--
Here is another variation on accepting a variable number of arguments. This allows for a variable
number of arguments to be passed to a Class constructor, as well as a customized class version to be
used dynamically. Syntax in code is:
$mail = Generator("MailClassName", $db_ref);
function Generator() {
$numargs = func_num_args();
$classname = func_get_arg(0);
$argstring='';
if ($numargs > 1) {
$arg_list = func_get_args();
for ($x=1; $x<$numargs; $x++) {
$argstring .= '$arg_list['.$x.']';
if ($x != $numargs-1) $argstring .= ',';
}
}
if (class_exists("Custom{$classname}")) {
$classname = "Custom{$classname}";
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
if ($argstring) return eval("return new $classname($argstring);");
return new $classname;
}
Hope this is of use to someone.
http://php.net/manual/en/function.func-get-args.php