Re: Callback functions inside classes - how to?

From: Date: Sat, 09 Oct 2004 10:27:10 +0000
Subject: Re: Callback functions inside classes - how to?
Groups: php.general 
Request: Send a blank email to php-general+get-199061@lists.php.net to get a copy of this message
From the manual: http://uk.php.net/manual/en/language.pseudo-types.php#language.types.callback If you want to change things in the callback, the function (or method) should accept its parameters by reference. e.g. class A { function name(&$a, $b, $c) { $a = $b . $c; return; } function parse() { // whatever // then (calls $this->name()) $parser->set_handler( "root/page/title", array($this, 'name') ); // or (calls A::name()) $parser->set_handler( "root/page/title", array('A', 'name') ); // more whatever } } Hope this helps Cheers Chris Thomas Hochstetter wrote:
Hi again, I have always been wondering how this is done properly: Here is an example: [snip] class A { function name( $a, $b, $c) {
     $tmp = array();
     $tmp[a] = $a;
     .
    array_push( $GLOBALS['XMLStack'], $tmp );
} function parse() { .. some definitions . $parser->set_handler( "root/page/title", "name" ); . some more stuff here . } } [/snip] What I want is to have the callback function name as it is in the above example. But, obviously, the above code won't work. So, how do I tell the set_handler function that it must use the name function from the class? Using: "A::name" or "$this->name" (if instantiated) . how do these callback function calls work, because the same issue is with the xml handler functions in php4 (have not as yet been to v5). Also, how can I get the data from the callback function out without using $GLOBALS? I cannot just return an array, can I? Any ideas. Thanks so long. Thomas


« previous php.general (#199061) next »