Re: $this in an XML data handler ... in a class
| From: | Peter Clarke | Date: | Wed, 03 Jul 2002 14:09:58 +0000 |
| Subject: | Re: $this in an XML data handler ... in a class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-105190@lists.php.net to get a copy of this message | ||
"Clay Loveless" <clay@killersoft.com> wrote in message
news:B9481008.158BF%clay@killersoft.com...
> Here's a brain-bender ... At least it is for me at the moment. : )
>
> When I use an XML parser inside a class, the xml_*_handler functions
aren't
> recognizing "$this->" variables. I can kind of see why ... But would like
it
> to work anyway. : )
>
> Here's an example:
>
> class Blah
> {
> var $xmlparser;
> var $current_element;
>
> // ...
>
> function _parseXML($data)
> {
> $this->xmlparser = xml_parser_create();
> xml_set_element_handler(
> $this->xmlparser,
> array($this,"_xml_start_element"),
> array($this,"_xml_end_element"));
> xml_set_character_data_handler(
> $this->xmlparser,
> array($this,"_xml_character_data"));
> xml_parse($this->xmlparser, $data);
> xml_parser_free($this->xmlparser);
> }
>
> function _xml_start_element($p, $e_name, $e_attributes)
> {
> $this->current_element = $e_name;
> }
>
> function _xml_end_element($p, $e_name)
> {
> // ...
> }
>
> function _xml_character_data($p, $data)
> {
> echo "element is: ".$this->current_element."\n";
> echo "data is: $data\n";
> }
>
> } // end of class Blah
>
>
>
> When this XML parser gets called from within the Blah class, the "element
> is:" portion of _xml_character_data comes out blank!
>
> This sort of makes sense, because the callback functions are "children" of
> the xml_parser_create "parent" ... But should that make the children
> ignorant of the "grandparent" variables referred to by $this->varname?
>
> I hope this makes sense ... Has anyone else encountered this sort of
> problem? I'm an old hat at PHP, but am relatively new to both XML parsing
> and writing my own classes.
>
> Thanks,
> Clay
>
Have a look at:
http://www.php.net/manual/en/function.xml-set-object.php
xml_set_object($this->parser, &$this);