note 20574 added to function.call-user-func

From: Date: Mon, 08 Apr 2002 17:34:35 +0000
Subject: note 20574 added to function.call-user-func
Groups: php.notes 
Request: Send a blank email to php-notes+get-28976@lists.php.net to get a copy of this message
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). 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)) -- http://www.php.net/manual/en/function.call-user-func.php http://master.php.net/manage/user-notes.php?action=edit+20574 http://master.php.net/manage/user-notes.php?action=delete+20574 http://master.php.net/manage/user-notes.php?action=reject+20574

« previous php.notes (#28976) next »