Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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