2

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Raphael Caixeta
  • 7,808
  • 9
  • 51
  • 76
  • 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 Answers1

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