From: ckrack at i-z dot de
Operating system: Any
PHP version: 5.1.1
PHP Bug Type: Feature/Change Request
Bug description: Dynamical creation of objects
Description:
------------
I'd like to request a function to be able to create objects dynamically.
I have a classname as string and an array containing parameters. The
parameters can be imagined as fetched from func_get_args().
I now want to create an object out of these two, similar to how i would
call a method or function using call_user_func_array().
There are three ways to implement this in PHP code, none of them are
nice:
- force classes to implement an 'init' function which receives the
paramarray instead of the constructor. bad, because classes need to be
changed.
- use eval to create a statement like "new $classname($param1, $param2,
$param3,....)". Eval however is bad and slow.
- use a switch to check how many params are available, write a statement
to create a class with each number of parameters. iirc there are about 180
params allowed (theoretically), so to cover all possibilitys, one would
have to write about 180 cases ;)
Reproduce code:
---------------
function make_object($name, $params)
{
// make object instance
if (is_array($params)) {
// make object with multiple parameters
switch(count($params)){
case 0:
$obj = new $name();
break;
case 1:
$obj = new $name($params[0]);
break;
case 2:
$obj = new $name($params[0], $params[1]);
break;
//etc,.......
default:
// DIRTY DIRTY but not possible in another way: we create the
statement to call the class with multiple params
$eval = "new ".$name."(";
$eval .= implode(",", $params);
$eval .= ")";
eval("\$obj = $eval;");
} // switch
} else {
// create object without parameters
$obj = new $name();
}
return $obj;
}
Expected result:
----------------
I want a function named something like "create_user_object_array" which
receives a classname and a array containing parameters which are passed to
the contructor of the class.
Actual result:
--------------
The provided code does what it should, but it is not in a nice style.
lacking the possibilities.
--
Edit bug report at http://bugs.php.net/?id=35747&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=35747&r=trysnapshot44
Try a CVS snapshot (PHP 5.1):
http://bugs.php.net/fix.php?id=35747&r=trysnapshot51
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=35747&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=35747&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=35747&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=35747&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=35747&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=35747&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=35747&r=support
Expected behavior: http://bugs.php.net/fix.php?id=35747&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=35747&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=35747&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=35747&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35747&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=35747&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=35747&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=35747&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=35747&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=35747&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=35747&r=mysqlcfg