This is a discussion on Create a class whos name is contained in a variable. within the PHP General forums, part of the PHP Programming Forums category; Hello all, I would like to be able to create a new instance of a class dynamicly from a variable ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello all,
I would like to be able to create a new instance of a class dynamicly from a variable name. Equivalent in java is: ClassController vController = null; String vClassName = "SomeControllerClass"; vController = (ClassController)Class.forName(vClassName).newInst ance(); vController.control(); Giving that each class is implementing ClassController and control() method. Thank you |
|
|||
|
ethorsen@gmail.com wrote:
> Hello all, > > I would like to be able to create a new instance of a class dynamicly > from a variable name. Equivalent in java is: > > ClassController vController = null; > String vClassName = "SomeControllerClass"; > vController = (ClassController)Class.forName(vClassName).newInst ance(); > vController.control(); > > Giving that each class is implementing ClassController and control() > method. > > Thank you The following works for me: $classType = 'MyClass'; $object = new $classType('constructor parameters'); var_dump($object); |
|
|||
|
is it that simple? man i love php. Thank you
mootmail-googlegroups@yahoo.com wrote: > ethorsen@gmail.com wrote: > > Hello all, > > > > I would like to be able to create a new instance of a class dynamicly > > from a variable name. Equivalent in java is: > > > > ClassController vController = null; > > String vClassName = "SomeControllerClass"; > > vController = (ClassController)Class.forName(vClassName).newInst ance(); > > vController.control(); > > > > Giving that each class is implementing ClassController and control() > > method. > > > > Thank you > > The following works for me: > > $classType = 'MyClass'; > $object = new $classType('constructor parameters'); > var_dump($object); |