I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.
For example: I have a situation where I have a script that can deal
with objects of one of several classes, but each class needs some
slightly different setup parameters. I'm currently taking care of
this with a switch statement, but i'd like to eliminate it if at all
possible. As it stands, my code is along the lines of:
$thisClass = 'FirstClass'; // Will come from an external source in
real life, validated of course!
switch ($thisClass)
{
case 'FirstClass' :
$val = 'first val';
break;
case 'SecondClass' :
$val = 'second val';
break;
case 'ThirdClass' :
$val = 'third val';
break;
// ...
}
echo ($val);
I'd rather do something along the lines of
class FirstClass
{
const VAL = 'First val';
}
//...
echo ($thisClass::VAL);
I'm guessing that there is some function that returns a class from a
string, but I can't find it. Can anyone help out here?