是否有一个函数可用于实例化带参数的对象?
#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');
答案 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);