I am trying to refer to a defined constant in a separate function.
The errors I am getting refer to the variable not defined and the variable defined as a constant for each FOO and BAR.
class Boo {
public function runScare() {
$this->nowScaring('FOO');
$this->nowScaring('BAR');
}
private function nowScaring($personRequest) {
define ('FOO', "test foo");
define ('BAR', "test bar");
$person = ${$personRequest};
echo "<br/>Query selected is: " . $person . "<br/>";
}
}
$scare = new Boo;
$scare->runScare();
Constants should be defined once only, at the top of your script, like this:
Then, to access them, don’t put their names in quotes:
If for some reason you want to get the value of the constant and all you have is its name in a variable, you can do so with the
constantfunction:In this particular case, it looks like that class constants might be a better fit: