I have a variable called $theclass and it's a string "Home_class". How can I define a class out of that string? The reason I need to do this is that the variable will change and I want to be able to declare the class that the variable is equal to.
Asked
Active
Viewed 184 times
2
-
possible duplicate of [instantiate a class from a variable in PHP?](http://stackoverflow.com/questions/534159/instantiate-a-class-from-a-variable-in-php) – user229044 Jun 12 '10 at 03:27
1 Answers
4
Use the new
keyword.
$theclass = 'My_class';
$x = new $theclass(); // $x is now an instance of My_class

user229044
- 232,980
- 40
- 330
- 338
-
that works perfecly, I have to wait 11 minutes to mark answer as correct though. Thanks. – Raphael Caixeta Jun 12 '10 at 03:20