开发者

PHP: how to make new object of class with name stored in member string?

开发者 https://www.devze.com 2023-03-30 21:55 出处:网络
So I want to do something开发者_如何学C like this: $ob = new $this->other_class_name; but it fails. How can I do it without storing other_class_name in local variable?Save the name in another va

So I want to do something开发者_如何学C like this:

$ob = new $this->other_class_name;

but it fails. How can I do it without storing other_class_name in local variable?


Save the name in another variable:

$class = $this->other_class_name;
$ob = new $class;


Try this:

$class = get_class($this->other_class_name);
$ob = new $class;
0

精彩评论

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