note 58999 added to function.xml-set-object

From: Date: Wed, 23 Nov 2005 16:28:08 +0000
Subject: note 58999 added to function.xml-set-object
Groups: php.notes 
Request: Send a blank email to php-notes+get-98982@lists.php.net to get a copy of this message
I finally managed to get xml_set_object() to work, after much documentation searching. As the solution I found has been hinted at, the focus has been wrong. There have been many problems getting class contained XML parsers to make changes to the members of an instance. This is because, from what I'm guessing, the functions are using a either a new annonymous instance of the class or an uninstanced version of the class. What we want to make sure it does it that the parser is accessing its handlers as member methods of a particular instance of a class. This can be done using the array method for passing a callback, setting the object as a reference to this instance. This way, you know that the parser is going to call the function properly. Ex: <?php class Parser { private $parser; private $data; public function __construct() { $this->parser = NULL; $this->data = ''; } private function ParseElementStart($parser, $name, $attrs) { // Code } private function ParseElementEnd($parser, $name) { // Code } public function Parse($XMLdata) { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, array(&$this, 'ParseElementStart'), array(&$this, 'ParseElementEnd')); xml_parse($this->parser, $XMLdata); xml_parser_free($this->parser); } } ?> With this, when you call the Parse method, the data in that instance can be modified. I'm not sure if xml_set_object becomes unneccissary when using the array callbacks, but I leave it in just to make sure that the xml_parse function knows it's in the object. As above, I'd recommend for memory sake, that the XML parser be created, used, and freed all in the same function, to ensure that everything is cleaned up correctly. ---- Manual Page -- http://www.php.net/manual/en/function.xml-set-object.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58999 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58999&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58999&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98982) next »