Re: [PHP] $this in an XML data handler ... in a class
Clay: On Wed, Jul 03, 2002 at 02:20:56AM -0700, Clay Loveless wrote: 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)); Without getting into all of the other potential issues in your code, allow me to quickly point out that the function name parameters to the set_*_handler() are supposed to be strings. The string is to be the name of the function. So, for example, do this: xml_set_character_data_handler($this-xmlparser, '_xml_character_data'); Now, I'm not guaranteeing this will cause the function to become part of the class, but at least the function will be properly initiated. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] $this in an XML data handler ... in a class
Actually, a careful reading of the docs reveals the following at the bottom of each xml_set_*_handler section: Note: Instead of a function name, an array containing an object reference and a method name can also be supplied. -Clay From: Analysis Solutions [EMAIL PROTECTED] Date: Wed, 3 Jul 2002 13:14:34 -0400 To: PHP List [EMAIL PROTECTED] Subject: Re: [PHP] $this in an XML data handler ... in a class Clay: On Wed, Jul 03, 2002 at 02:20:56AM -0700, Clay Loveless wrote: 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)); Without getting into all of the other potential issues in your code, allow me to quickly point out that the function name parameters to the set_*_handler() are supposed to be strings. The string is to be the name of the function. So, for example, do this: xml_set_character_data_handler($this-xmlparser, '_xml_character_data'); Now, I'm not guaranteeing this will cause the function to become part of the class, but at least the function will be properly initiated. --Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] $this in an XML data handler ... in a class
In a follow up on this, here's something else that's kind of bizzare ... Within this class example, if I add a variable declaration of: var $testval = 'this is a test'; And then add to _xml_character_data(): echo TEST: $this-testval\n; ... I find that within the class structure, _xml_character_data can READ the $this-testval values (set outside of any callback function), but apparently the _xml_start_element() callback function cannot SET $this-current_element. My output is: element is: data is: [valid data] TEST: this is a test Is this a bug? It's beginning to have the feel of one... -Clay 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] $this in an XML data handler ... in a class
Clay: On Wed, Jul 03, 2002 at 11:05:34AM -0700, Clay Loveless wrote: Note: Instead of a function name, an array containing an object reference and a method name can also be supplied. Interesting. Thanks! Anyway, back to your situation. I put together a test. Two counters are running and get displayed each time each function is called. One counter is a regular variable which I bring into each function via a global statement. The other counter is part of the object. Interestingly, in this case, the object variables are not acting as if they are part of the class, rather they're behaving as if their scope is stuck within each function. As far as parsing XML, be aware that the character data handler get's called for each bit of non-tag data, including white spaces in tags and between tags. And, character data can contain multiple lines but they get passed through the character data handler function one line at a time, not all at once. So, performing maneuvers in the character_handler function is tricky. I save my character data in an array and then implode the array in the end handler function. This process is in the test, below, as well. I've got a PHP XML expat parsing tutorial up on the web that may prove helpful: http://www.analysisandsolutions.com/code/phpxml.htm #! /usr/local/bin/php -q ?php class Blah { var $xmlparser; var $current_element; var $count = 0; function _parseXML($data) { global $g; $g = 0; $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) { global $CData, $g; $CData = array(); echo 'g:' . ++$g . ' o:' . ++$this-count . start\n; echo start element: $e_name\n; } function _xml_character_data($p, $data) { global $CData, $g; $CData[] = $data; echo 'g:' . ++$g . ' o:' . ++$this-count . character\n; echo character data: $data\n; } function _xml_end_element($p, $e_name) { global $CData, $g; echo 'g:' . ++$g . ' o:' . ++$this-count . end\n; echo end element: $e_name\n; echo end data array: . trim( implode('', $CData) ) . \n; } } // end of class Blah $XML = ' doc item Some Item Text /item /doc '; echo $XML\n; $Class = new Blah(); $Class-_parseXML($XML); ? Enjoy, --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] $this in an XML data handler ... in a class
Dan, Thanks for your reply ... Glad to be helpful on the one tidbit I uncovered! Looks like your conclusion is the same as mine: the object variables are readable within the handler functions, but they are not writeable. Hence your use of the global variables ... That seems to be the only workaround. I'm convinced this has to be a bug ... Because what good is a contained class if you've got to interact with global variables in order to get the job done? Theoretically speaking, how do you know that you're not stepping on the toes of some other global variable? This solution may be good enough for me and you ... If we're not writing classes for distribution ... But they go against the grain of all the bennies I've read about classes. I'm not normally one to cry 'BUG!' ... But I think this qualifies. Do you agree? -Clay From: Analysis Solutions [EMAIL PROTECTED] Date: Wed, 3 Jul 2002 16:38:38 -0400 To: PHP List [EMAIL PROTECTED] Subject: Re: [PHP] $this in an XML data handler ... in a class Clay: On Wed, Jul 03, 2002 at 11:05:34AM -0700, Clay Loveless wrote: Note: Instead of a function name, an array containing an object reference and a method name can also be supplied. Interesting. Thanks! Anyway, back to your situation. I put together a test. Two counters are running and get displayed each time each function is called. One counter is a regular variable which I bring into each function via a global statement. The other counter is part of the object. Interestingly, in this case, the object variables are not acting as if they are part of the class, rather they're behaving as if their scope is stuck within each function. As far as parsing XML, be aware that the character data handler get's called for each bit of non-tag data, including white spaces in tags and between tags. And, character data can contain multiple lines but they get passed through the character data handler function one line at a time, not all at once. So, performing maneuvers in the character_handler function is tricky. I save my character data in an array and then implode the array in the end handler function. This process is in the test, below, as well. I've got a PHP XML expat parsing tutorial up on the web that may prove helpful: http://www.analysisandsolutions.com/code/phpxml.htm #! /usr/local/bin/php -q ?php class Blah { var $xmlparser; var $current_element; var $count = 0; function _parseXML($data) { global $g; $g = 0; $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) { global $CData, $g; $CData = array(); echo 'g:' . ++$g . ' o:' . ++$this-count . start\n; echo start element: $e_name\n; } function _xml_character_data($p, $data) { global $CData, $g; $CData[] = $data; echo 'g:' . ++$g . ' o:' . ++$this-count . character\n; echo character data: $data\n; } function _xml_end_element($p, $e_name) { global $CData, $g; echo 'g:' . ++$g . ' o:' . ++$this-count . end\n; echo end element: $e_name\n; echo end data array: . trim( implode('', $CData) ) . \n; } } // end of class Blah $XML = ' doc item Some Item Text /item /doc '; echo $XML\n; $Class = new Blah(); $Class-_parseXML($XML); ? Enjoy, --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] $this in an XML data handler ... in a class
Hey Clay: On Wed, Jul 03, 2002 at 03:25:42PM -0700, Clay Loveless wrote: Looks like your conclusion is the same as mine: the object variables are readable within the handler functions, but they are not writeable. I don't think that's an accurate description. The object variables are writiable, but their scope remains within the particular funciton. So, in essence, in my test, there are three $this-count variables floating around -- one for each function. I'm convinced this has to be a bug ... Because what good is a contained class if you've got to interact with global variables in order to get the job done? Well, it may or may not be a bug. This strange behavior is probably due to the special nature of the XML functions. They don't really seem to be integrated into the class. Other user defined methods inside classes still behave as usual. With the scope of object variables being global to the object and all methods therein. Further clarification on this subject need to be provided by someone with a better understanding PHP's inner workings. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php