jorgep Posted November 27, 2007 Share Posted November 27, 2007 Hi guys, I'm working on a MVC Framework for PHP, inspired on Ruby on Rails. I got it working right now, but I'm testing it with a personal project. I came up with a question... Is it possible to instance a class from an string? Here is what I have: I have a main abstract class that is called 'manager', for every table on the database I have an extended class of 'manager' with the name of the table, like this: manager.class.php - users.php (for table users) - owners.php - admins.php - cities.php - etc... What I would like is instantiate one of these classes from a string, an string could be a parameter passed by POST or GET or any string, something like this: $string = 'users'; //This is supposed to be a dynamic string $dynamic_class = new $string; //Then dinamyc_class is an instance of 'users' Something like that is what I need. Let me know if this is possible. Soon I will send the site to beta test it ... Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/79122-solved-dynamic-instance-of-class-is-this-possible/ Share on other sites More sharing options...
jorgep Posted November 27, 2007 Author Share Posted November 27, 2007 Ok crap, as soon as I wrote that post, I realized that I haven't tested that option (LOL)... this is working now :S I don't know how hehehe: <?php require_once("knife_config.php"); require_once(ROOT."def/admins.php"); $class = "admins"; $dynamic_class = new $class; echo $dynamic_class->table_name; echo $dynamic_class->render_form_from_structure(); ?> Thanks anyway hehehe. Link to comment https://forums.phpfreaks.com/topic/79122-solved-dynamic-instance-of-class-is-this-possible/#findComment-400479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.