Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-根据var创建具有所需类型的类对象_Php_Class - Fatal编程技术网

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

PHP-根据var创建具有所需类型的类对象,php,class,Php,Class,是否仍然可以基于var值创建类对象类型 我想做的示例: 假设我有几十个班:A班、B班和C班,。。。这些都是ClassBasic类的扩展。因此,它们具有相同的接口函数doOperation(),但内部实现不同 // the variable contains the name of the desired class type. $TypeToCreate = "ClassC"; // create the desired type of class $ClassObj = new $TypeT

是否仍然可以基于var值创建类对象类型

我想做的示例:

假设我有几十个班:A班、B班和C班,。。。这些都是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();
对不起,但mpm是对的, 它起作用了。没关系

我还将查看因子模式,以了解它是关于什么的


谢谢

这就是解决$ClassObj=new$TypeToCreate应该起作用的地方,问题是什么?使用工厂模式,或者参见