Re: [PHP] callbacks to methods inside a class/object

2002-09-17 Thread Tom Rogers

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", "$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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] callbacks to methods inside a class/object

2002-09-17 Thread Douglas Marsh


Is there a "clean" way to make use of PHP builtins that use callbacks and 
point those call backs to a method inside the class/object:

A good example would be:

...

class XMLClass {

  var $parser;

  function XMLClass() {
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, TRUE);
xml_set_element_handler($this->parser, "$this->start", "$this->end");
xml_set_character_data_handler($this->parser, "$this->data");
  }

  function goodbye() { // a manual destructor
xml_parser_free($this->parser);
// other things possibly too
  }


  function start($p2, $name, $attr) {
// do things here
  }

  function data($p2, $data) {
// do some more here
  }

  function end($p2, $name) {
// do even more things here
  }

  [... and so on ...]

...

But since there is no way to set a callback to "$this->[function_name]" one 
must create a global function that uses a global object and passes things to 
the method inside the class..

Is the a way to address this? or perhaps a better way to deal with callback 
function names?

--Douglas Marsh






_
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php