checking if a function exists before doing an eval
| From: | Henry | Date: | Thu, 08 Aug 2002 11:10:05 +0000 |
| Subject: | checking if a function exists before doing an eval | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-111624@lists.php.net to get a copy of this message | ||
Hi All,
I'm trying to evaluate a function (but only if it exists!).
something like (pseudo code):
function animal_is_bear($name)
{
return $name."is a big grizzley bear";
}
function animal_is_goat($name)
{
return $name."is a wee mountain goat";
}
$animal_type="goat";
$animal_name="billy";
echo eval("return animal_is_".$animal_type."(".$animal_name.");");
$animal_type="puma";
$animal_name="sinbad";
echo eval("return animal_is_".$animal_type."(".$animal_name.");");
The first eval should return "billy is a wee mountain goat";
But the last eval causes a problem because the function animal_is_puma does
not exist.
How do I check if the funciton exists before I eval?
Or am I better of having an associative array of functions?
TIA
Henry