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. Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.