0

I'm looking for a way to create a new object with the help of a variable, but instead of referencing it by the variable I want to reference it by the value of the variable.

$name = "moduleAjax";
$name = new $name;

And instead of doing this,

$name->method();

I would do this,

$moduleAjax->method();

I don't have a live example of my code. I've been experimenting a little bit but I can't find a solution.

Any help would be greatly appreciated.

4

1 回答 1

3

您搜索变量变量: http: //php.net/manual/en/language.variables.variable.php

所以写:

$name = "moduleAjax";
$$name = new $name; // a dollar sign was added to $name

moduleAjax然后,您可以访问这个新创建的以变量命名的类实例$moduleAjax

于 2013-04-20T20:00:42.880 回答