基于变量创建对象的新实例


Create new instance of object based on variable

我试图创建一个对象的实例,但对象类名是手工设置的。

在config.php中:

define('DIRECTORY', 'RaptorDirectory');

在类文件中:

$this->directory = new DIRECTORY; // <--- how do I use the constant there?

我这样做是因为DIRECTORY可能会更改为不同的类(例如LDADirectory)

不能在那里使用常量,但可以使用变量,例如

$class = DIRECTORY;
$this->directory = new $class;