Hi all,
I have a script in which an object should
be able to read its data from a xml file, like this
class Car {
// ...
function read($filename) {
$xmlParser = xml_parser_create();
xml_set_element_handler($xmlParser, "\$this->start", "\$this->end");
xml_set_character_data_handler($xmlParser, "\$this->chars");
$in = fopen($filename, "r"); // Open the input stream
// Parse the file
while ($data = fread($in, 4096)) {
if (!xml_parse($xmlParser, $data, feof($in))) {
return false; // On error return
}
}
xml_parser_free($xmlParser);
}
function start($parser, $tag, $attr) {
// handle start of element event here
}
function end($parser, $tag) {
// handle end of element event here
}
function chars($parser, $data) {
// handle data event here
}
}
$car = new Car;
$car->read("ford.xml");
when I run the script get errors for the handlers like this:
Warning: Unable to call handler $this->start() in /xx/xx.php on line
Warning: Unable to call handler $this->chars() in /xx/xx.php on line
Warning: Unable to call handler $this->end() in /xx/xx.php on line **
I also tried without $this-> like this:
xml_set_element_handler($xmlParser, "start", "end");
xml_set_character_data_handler($xmlParser, "chars");
but I get similar errors.
When the handlers are global functions it works fine,
but I absolutely want the handler functions to be methods
of the class.
How can I set the handlers then? I am sure there must
be a way of doing what I want, but I could not find
any example. Any hint will be very appreciated.
I am using php 4.1.1 on Linux.
Regards,