Bijou Trouvaille Bijou Trouvaille - 1 year ago 72
PHP Question

In PHP 5 can I instantiate a class dynamically?

Is it possible to dynamically instantiate a class using a variable? For example is something like this possible in PHP?

class foo
public $something;

$class_name = "foo";

$f = new $class_name();

Answer Source

That should work, yes.

You can also do:

$f = new $class($arg1,$arg2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download