scottnicol Posted April 12, 2010 Share Posted April 12, 2010 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 More sharing options...
Matthias_ Posted April 12, 2010 Share Posted April 12, 2010 $sClass = "system/controllers/{$id}_Controller.php"; include ($sClass); Try that? Link to comment https://forums.phpfreaks.com/topic/198254-use-variable-as-class-name/#findComment-1040217 Share on other sites More sharing options...
trq Posted April 12, 2010 Share Posted April 12, 2010 Your code is syntactically correct (except you don't actually need to braces after the class name), what exactly is the problem? Link to comment https://forums.phpfreaks.com/topic/198254-use-variable-as-class-name/#findComment-1040222 Share on other sites More sharing options...
scottnicol Posted April 12, 2010 Author Share Posted April 12, 2010 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? Link to comment https://forums.phpfreaks.com/topic/198254-use-variable-as-class-name/#findComment-1040225 Share on other sites More sharing options...
trq Posted April 12, 2010 Share Posted April 12, 2010 Why would you need to have the variable named after the controller? Link to comment https://forums.phpfreaks.com/topic/198254-use-variable-as-class-name/#findComment-1040262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.