Normally, I would have to do:
$str = "classname"; //classname is the name of a class
switch($str)
{
case "class1":
class1::run(); break;
case "class2":
class2::run(); break;
case "class3":
class3::run(); break;
case "class4":
class4::run(); break;
case "classname":
classname::run(); break;
default:
die("Fatal error: no such class");
}
//in my case, there are hundreds of cases and more being added
Is there a way of simplifying this, perhaps in one line? Wishful thinking?
$str::run()
Of course, it would be my responsibility to make sure $str is actually the name of an object. But managing that would be much easier than managing a huge list like above.
Is this possible?
Yes; as you guessed, $str::run();
works.
php> class A { public static function run() { echo "A!"; } }
php> class B { public static function run() { echo "B!"; } }
php> $n = "A";
php> $n::run();
A!
php> $n = "B";
php> $n::run();
B!
you could do something like:
if (class_exists($str) && method_exists($str,'run')){
{$tr}::run();
}