问题
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();
回答1:
That should work, yes.
You can also do:
$f = new $class($arg1,$arg2);
回答2:
Yes, this code will work fine.
回答3:
In PHP 5 can I instantiate a class dynamically?
Yes you can, your code should work fine.
回答4:
Yes of course you can instantiate using dynamic names;
来源:https://stackoverflow.com/questions/3112727/in-php-5-can-i-instantiate-a-class-dynamically