2011-06-29 37 views
1

我试图创建一个对象的实例,但手动设置对象类名称。基于变量创建对象的新实例

在config.php:

define('DIRECTORY', 'RaptorDirectory'); 
在类文件

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

我这样做是因为目录可能更改为不同的类别(例如,LDAPDirectory)

回答

3

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

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

谢谢。将只是切换到我猜的变量。这是记录? – davidosomething

+0

@davidosomething http://www.php.net/manual/en/language.oop5.basic.php#example-157 – Phil