php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13553 $this isn't set in XML object handlers
Submitted: 2001-10-04 22:59 UTC Modified: 2002-01-14 22:56 UTC
From: phpbugs at improbable dot org Assigned:
Status: Closed Package: XML related
PHP Version: 4.0.6 OS: Mac OS X 10.1
Private report: No CVE-ID: None
 [2001-10-04 22:59 UTC] phpbugs at improbable dot org
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().


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-14 10:01 UTC] lobbin@php.net
Is this valid on 4.1.1?
 [2002-01-14 22:56 UTC] phpbugs at improbable dot org
It no longer crashes with 4.1.0. I'm going to test my larger XML application later this week.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Dec 03 08:03:33 2021 UTC