Jump to content

Use variable as class name?


scottnicol

Recommended Posts

I'm creating a basic MVC framework, but I'm stuck on one thing, and that is trying to get a class to be assigned to a variable, where the class name is a variable...

 

        include("system/controllers/".$id."_Controller.php");
$controller = new $id();

 

$id is the name of the class.

 

Link to comment
https://forums.phpfreaks.com/topic/198254-use-variable-as-class-name/
Share on other sites

It works fine, but I'm just wondering if it's possible to change $controller to the value of $id.

 

$id = home;

$home = $controller;

 

I did this:

include("system/controllers/".$id."_Controller.php");
${$id} = new $id;

 

and when I use

 

$home->showTest();

 

It works. Would that be considered okay to use?

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.