ID: 13553
Updated by: lobbin
Old Summary: $this isn't set in XML object handlers
Reported By: [EMAIL PROTECTED]
Old Status: Open
Status: Feedback
Bug Type: XML related
Operating System: Mac OS X 10.1
PHP Version: 4.0.6
New Comment:

Is this valid on 4.1.1?


Previous Comments:
------------------------------------------------------------------------

[2001-10-04 22:59:47] [EMAIL PROTECTED]

xml_set_object() allows you to use an xml parser inside a 
class and have it call class methods instead of global 
functions. $this is not set properly within the context of 
these handlers when called by the xml parser. This can 
cause data loss (e.g. $this->variable is set by one of the 
XML handlers and disappears after the parsing is finished) 
or, if you try to call certain functions on $this such as 
gettype() or classname() php will die with a bus error:

<?
        class ThisBug {
                var $XMLParser;
                
                function ThisBug() {
                        $this->XMLParser = xml_parser_create();

                        xml_set_object($this->XMLParser, $this);
                        xml_parser_set_option($this->XMLParser, 
XML_OPTION_CASE_FOLDING, false);
                        xml_set_element_handler($this->XMLParser, 
"xmlStartElement", "xmlEndElement");
                }
                        
                function parse() {
                        $xml = '<?xml version="1.0"?' . '><foo><bar></bar></foo>
';              
                
                        if (!xml_parse($this->XMLParser, $xml, true)) {
                                        die( sprintf("XML parse error: %s at line 
%d\n", 
xml_error_string(xml_get_error_code($this->XMLParser)), 
xml_get_current_line_number($this->XMLParser) )
                                );
                        }
                        xml_parser_free($this->XMLParser);
                }
        
                function xmlStartElement($parser, $name, $attribs) {
                        print "Opening $name - \$this = " . gettype($this) . "\
n";
                }

                function xmlEndElement($parser, $name) {
                        print "Closing $name - \$this = " . gettype($this) . "\
n";
                }

        }

        $CrashMe = new ThisBug();
        $CrashMe->parse();
?>


chris@calaban:~/Development/netdiff $ php -q ThisBug.php 
Opening foo - $this = object
Opening bar - $this = object
Closing bar - $this = object
Bus error

(uname -a = Darwin calaban.my.domain 1.4 Darwin Kernel 
Version 1.4: Sun Sep  9 15:39:59 PDT 2001; root:xnu/xnu-
201.obj~1/RELEASE_PPC  Power Macintosh powerpc)

IMPORTANT NOTE:
This appears to be identical to bug #12959, which was 
marked bogus with the mistaken comment "you are trying to 
use $this outside of a class so it does not work.", when 
$this was being used inside a class, as it is here. This is 
the entire point for having xml_set_object().



------------------------------------------------------------------------



Edit this bug report at http://bugs.php.net/?id=13553&edit=1


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to