note 94396 added to language.variables.variable
| From: | (mwwaygooAThotmailDOTcom) | Date: | Mon, 02 Nov 2009 16:03:32 +0000 |
| Subject: | note 94396 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162338@lists.php.net to get a copy of this message | ||
A note on Variable variables/functions and classes
To store a function name in a variable and call it later, within a class, you do the following:-
<?php
class test_class
{
var $func='display_UK'; // function name *
function display_UK()
{
echo "Hello";
}
function display_FR()
{
echo "Bonjour";
}
function display()
{
$this->{$this->func}(); // NOTE the brackets MUST be here and not in the function name above
*
}
}
$test=new test_class();
$test->display_UK(); // to test they work directly
$test->display_FR();
$test->display();
?>
This allows you to specify the function required. It works better then a big switch statement as it
allows for extending the class more easily. (ie adding display_ES(); )
----
Server IP: 69.147.83.197
Probable Submitter: 84.12.100.124
----
Manual Page -- http://www.php.net/manual/en/language.variables.variable.php
Edit -- https://master.php.net/note/edit/94396
Del: integrated -- https://master.php.net/note/delete/94396/integrated
Del: useless -- https://master.php.net/note/delete/94396/useless
Del: bad code -- https://master.php.net/note/delete/94396/bad+code
Del: spam -- https://master.php.net/note/delete/94396/spam
Del: non-english -- https://master.php.net/note/delete/94396/non-english
Del: in docs -- https://master.php.net/note/delete/94396/in+docs
Del: other reasons-- https://master.php.net/note/delete/94396
Reject -- https://master.php.net/note/reject/94396
Search -- https://master.php.net/manage/user-notes.php