note 20574 deleted from function.call-user-func by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 09:01:22 +0000 |
| Subject: | note 20574 deleted from function.call-user-func by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196828@lists.php.net to get a copy of this message | ||
Note Submitter: teleware at mind dot net
----
I ran into some quirky behavior using call_user_func with an object. The function parseAction()
below lets me call any of my object's methods by passing the name of the method into a page
(e.g. http://xyz.com/page.php?action=login).
<?php
class MyClass {
function parseAction($actionKey)
{
global $METarget;
if(method_exists($this,$actionKey))
{
call_user_func(array(&$this,$actionKey));
return;
}
else
{
exit("Unknown action keyword: $actionKey");
}
}
...
// other methods
...
}
?>
I found that my member variables weren't always being updated correctly. The numberic values
were preserved but the string variables retained their old value. The solution was to put the
reference operator '&' in front of the object's variable. In this case it's
$this because I'm calling it from within the object but this was also true when calling
parseAction from an instanciated object e.g. call_user_func(array(&$myObject,$actionKey))