Create a class whos name is contained in a variable.

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-04-2006
ethorsen@gmail.com
 
Posts: n/a
Default Create a class whos name is contained in a variable.

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

Reply With Quote
  #2 (permalink)  
Old 10-04-2006
mootmail-googlegroups@yahoo.com
 
Posts: n/a
Default Re: Create a class whos name is contained in a variable.

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);

Reply With Quote
  #3 (permalink)  
Old 10-04-2006
ethorsen@gmail.com
 
Posts: n/a
Default Re: Create a class whos name is contained in a variable.

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);


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0