note 57370 added to function.xml-parser-free

From: Date: Sat, 01 Oct 2005 16:56:36 +0000
Subject: note 57370 added to function.xml-parser-free
Groups: php.notes 
Request: Send a blank email to php-notes+get-96143@lists.php.net to get a copy of this message
If you try to free an XML parser while it is still parsing, strange things will happen - your script will "hang" and no output will be sent to the browser. Consider this pseudo-code example: ------- ... if (!xml_parse($parser)) echo 'XML error'; xml_parser_free($parser); ... function SomeCallbackWhichWasSetBefore(...) { global $parser; ... if (some_error_happened) xml_parser_free($parser); //problem! ... } ------ It would be logical that xml_parse would return false if the parser was freed while parsing, right? Wrong! Instead, everything hangs and no output will be sent out (no matter whether output buffering is on or not). It took me more than an hour to figure out why: you cannot free a parser handle that is currently parsing. A simple solution: ------- $xml_error = false; if (!xml_parse($parser)) echo 'XML error (directly from parser)'; else if ($xml_error) echo 'XML error (from some callback function); xml_parser_free($parser); ... function SomeCallbackWhichWasSetBefore(...) { global $parser; global $xml_error; if ($xml_error) return; ... if (some_error_occured) { $xml_error = false; return; } ... } ------- If you use this solution you will have to check for $xml_error in every callback function. Essentially what you're doing is that, in case you want to stop parsing because of an error, you continue until xml_parse() is finished (without actually doing anything) and then free the parser and act on the error. Of course the underlying problem is that you cannot stop a parser while it is parsing. There should be some function like xml_throw_error() or xml_parser_stop() or whatever, but unfortunately there isn't. ---- Manual Page -- http://www.php.net/manual/en/function.xml-parser-free.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57370 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57370&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57370&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#96143) next »