So, your code does work. Just not quite how you think it does (or maybe want/expect it to).
Using variables as class/function names
You can do this, pretty much as you have done. For example:
$getTotal = "array_sum";
$array = [1,2,3,4,5,6,7];
echo $getTotal($array); // 28
You can do the same for a class
class test
{
public static $count = 0;
public $variable1 = "some var";
public function __construct()
{
test::$count++;
}
}
$className = 'test';
new $className;
echo test::$count; // 1
The problem with the above code is that you haven't assigned the class object
to a variable and so it is lost to the abyss.
So you need to assign it to a variable:
$myClassInstance = new $className;
echo test::$count; // 2 :: because it's the second time we've called the class
// and whether or not we keep the class in memory the
// static variable is updated; because it is static!
This is helpful if you need to assign a class based off of some dynamic input... But in general terms it's best to stick to the class name!
$anotherClassInstance = new test;
echo test::$count; 3;