创建对象的功能?

时间:2010-02-21 07:48:33

标签: php

是否有一个函数可用于实例化带参数的对象?

#include <database.h>
class database
{
    function __construct($dbhost, $user, $pass, $etc) { /* etc */ }
    function query($sql) { /* dowork*/ }
}
$args = array('localhost', 'user', 'pass', 'etc');

$db = create_object('database', $args); // is there a function like this?
$db->query('SELECT * FROM poop');

1 个答案:

答案 0 :(得分:1)

您可以使用ReflectionClass::newInstanceArgs

class database
{
    function __construct($dbhost, $user, $pass, $etc) { /* etc */ }
    function query($sql) { /* dowork*/ }
}
$args = array('localhost', 'user', 'pass', 'etc');

$ref = new ReflectionClass('database');
$db = $ref->newInstanceArgs($args);