php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #35747 Dynamical creation of objects
Submitted: 2005-12-20 17:34 UTC Modified: 2005-12-20 17:51 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ckrack at i-z dot de Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.1.1 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ckrack at i-z dot de
New email:
PHP Version: OS:

 

 [2005-12-20 17:34 UTC] ckrack at i-z dot de
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-20 17:51 UTC] tony2001@php.net
You can use Reflection API for that.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 01:01:32 2025 UTC