The subject says it all. Is it possible to load classes dynamically in PHP? In other words, can I say new $foo, where $foo is a string containing the name of a class? Hmm...
Ok, that answers that... it turns out it's possible to say "new $foo", but not "new "Foo"".
The following works and prints the constructor's message:
<?php
class Foo{
function Foo(){
echo "Foo constructor called!<br>";
}
}
$foo = "Foo";
$f = new $foo;
?>
But if the last line is $f = new "Foo", it doesn't.
Anyway, this is good enough for my purposes. I wanted to be able to load classes dynamically in my CMS, based on information in a configuration file. If I couldn't do this, I would have gotten around it by dynamically calling a function, which I was sure PHP could do.
Hey, and it's not even case sensitive
I can say $f = new $foo; where $foo is "foo".
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):