PHP - 根据var创建具有所需类型的类对象

时间:2014-01-15 15:34:54

标签: php class

是否有基于var值创建类对象类型

我想做的例子:

假设我有几十个类:ClassA,ClassB和ClassC,......都是ClassBasic类的扩展。因此它们具有相同的接口函数doOperation(),但具有不同的内部实现。

// the variable contains the name of the desired class type.
$TypeToCreate = "ClassC";

// create the desired type of class
$ClassObj = new $TypeToCreate;   

// do some operation of the created class.
$ClassObj->doOperation();

1 个答案:

答案 0 :(得分:0)

抱歉,但是mpm是对的, 有用。好吧。

我还会看一下因子模式,看看它是什么。

谢谢!