Jump to content

[SOLVED] Dynamic Instance of Class. Is this possible?


jorgep

Recommended Posts

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.