Variable function names
| From: | Jay D. Mumper | Date: | Tue, 11 Jul 2000 18:17:01 +0000 |
| Subject: | Variable function names | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-5968@lists.php.net to get a copy of this message | ||
Hey folks-
I would like to be able to do something like this, rather than
use a switch structure:
file_a.inc:
<?
function a() {
return "a";
}
------
Test_it.inc
<?
class Test_it {
var $value;
function Test_it ( $f ) {
include ("file_".$f.".inc");
$this->value = ${$f}(); //<<<<< Here's what I want
}
}
------
<?
include ("Test_it.inc");
$object = new Test_it("a");
?>
------
My one hangup is the function name - can it be variable?
And if so, what syntax do I need?
My other choice is to make the constructor look like this:
function Test_it ( $f ) {
switch ($f) {
a) include ("file_a.inc");
$this->value = a();
break;
b) include ("file_b.inc");
$this->value = b();
break;
}
}
--
Jay Mumper
Consultant / Anteil, Inc.
http://www.anteil.com