Re: callbacks to methods inside a class/object
| From: | Tom Rogers | Date: | Wed, 18 Sep 2002 04:16:29 +0000 |
| Subject: | Re: callbacks to methods inside a class/object | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116684@lists.php.net to get a copy of this message | ||
Hi,
Wednesday, September 18, 2002, 7:15:36 AM, you wrote:
DM> Is there a "clean" way to make use of PHP builtins that use callbacks and
DM> point those call backs to a method inside the class/object:
DM> A good example would be:
DM> ...
DM> class XMLClass {
DM> var $parser;
DM> function XMLClass() {
DM> $this->parser = xml_parser_create();
DM> xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, TRUE);
DM> xml_set_element_handler($this->parser, "$this->start",
DM> "$this->end");
DM> xml_set_character_data_handler($this->parser, "$this->data");
DM> }
DM> function goodbye() { // a manual destructor
DM> xml_parser_free($this->parser);
DM> // other things possibly too
DM> }
DM> function start($p2, $name, $attr) {
DM> // do things here
DM> }
DM> function data($p2, $data) {
DM> // do some more here
DM> }
DM> function end($p2, $name) {
DM> // do even more things here
DM> }
DM> [... and so on ...]
DM> ...
DM> But since there is no way to set a callback to "$this->[function_name]" one
DM> must create a global function that uses a global object and passes things to
DM> the method inside the class..
DM> Is the a way to address this? or perhaps a better way to deal with callback
DM> function names?
DM> --Douglas Marsh
Here is a skeleton of a parser class, note the uses of '&' to avoid
generating copies of the object:
class xml_parser {
var $xml_parser;
// constructor
function xml_parser() {
$this->xml_parser = xml_parser_create();
xml_set_element_handler($this->xml_parser,
array(&$this,"start_element"),array(&$this,"end_element"));
xml_set_character_data_handler($this->xml_parser,array(&$this,"character_data"));
}
function character_data($parser, $data) {
}
function start_element($parser, $name, $attrs) {
}
function end_element($parser, $name) {
}
function parse() {
}
}
//usage
$xml =& new xml_parser();
--
regards,
Tom