On Thu, 2002-03-07 at 06:25, Matt Williams wrote:
> Hi all
>
> I get a variable that is set be a select box on a page.
> I want to use this variable to call a function within a class that is based
> on it's name.
>
> so if $var = "me"
>
> I want to call $class->me_function();
>
> or if
>
> $var = "you"
>
> call
>
> $class->you_function();
>
> How can I use the variable as part of the function name to call.
>
> TIA
>
> matt
You can do this quite simply with variable function names. Essentially,
create a variable with the name of the method you want to call, and use
that where you would otherwise use the method name in the call: i.e. if
you have the name of a function in $funcname, you would do
$obj->$funcname(). Here's a full example:
<?php
error_reporting(E_ALL);
class foo {
function me_foo() {
echo "Me Foo!\n";
}
function you_foo() {
echo "You Foo!\n";
}
}
$foo = new foo;
$test = 'me';
// The direct way:
$foo->{$test . '_foo'}();
// Slightly less direct but perhaps more readable:
$method = $test . '_foo';
$foo->$method();
$test = 'you';
// The direct way:
$foo->{$test . '_foo'}();
// Slightly less direct but perhaps more readable:
$method = $test . '_foo';
$foo->$method();
?>
Hope this helps,
Torben
--
Torben Wilson <[EMAIL PROTECTED]>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php