creating class in runtime

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

creating class in runtime

Post by zyklone »

hello there.. i just want to ask..is it possible to create a CLASS in run time?

example:

$class = 'myclass';

$instance = new $class();

in which my myclass is not yet built.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to use eval(), or possibly reflection if you're using PHP5.
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

are you trying to create a class code and dump into a php file using/by running another php script?

if that is the case then please take a look at this script:

http://wsdl2php.svn.sourceforge.net/vie ... iew=markup

it reads the SOAP (XML) schema and generates php code and writes it into new php files.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Generally speaking, if you need to dynamically create a class, there's something wrong. There should be a way to build what you want without needing to generate a new class.
Styx
Forum Newbie
Posts: 9
Joined: Thu Jun 21, 2007 5:37 am

Post by Styx »

Post Reply