0
votes

Is there anyway to create a class object type based on var value ?

Example for what I would like to do:

Say I have some dozens of classes: ClassA, ClassB and ClassC,... that are all extends of ClassBasic class. So they have the same interface function doOperation(), but different internal implementation.

// 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
This is what the Factory Pattern solvesJohn Conde
$ClassObj = new $TypeToCreate should work , what is the issue?mpm
Use Factory Pattern, or see thisIstván Őri

1 Answers

0
votes

Sorry, but mpm was right, it works. And its ok.

I also will take a look for the Factor Pattern in order to see what is it about.

Thanks!