开发者

I want to call a class object, being the class name a given parameter

开发者 https://www.devze.com 2022-12-25 15:55 出处:网络
I want to do something like this: (in php) $a开发者_如何学JAVA = \"class_name1\"; $b = \"class_name2\";

I want to do something like this: (in php)

$a开发者_如何学JAVA = "class_name1";    
$b = "class_name2";
$object1 = new $a;
$object2 = new $b

is this possible?


Yes:

class A {
   public function foo(){
       echo 'bar';
   } 
}

$a = 'A';
$object = new $a();
$object->foo();

outputs

bar

You can test such things by yourself very fast on codepad.

0

精彩评论

暂无评论...
验证码 换一张
取 消