note 20574 modified in function.call-user-func by vrana

From: Date: Tue, 17 Aug 2004 14:53:32 +0000
Subject: note 20574 modified in function.call-user-func by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80086@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). <?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)) --was-- 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://php.net/manual/en/function.call-user-func.php

« previous php.notes (#80086) next »