php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12959 XML methods don't have $this set!
Submitted: 2001-08-25 09:41 UTC Modified: 2001-09-01 14:22 UTC
From: slowbyte at hot dot ee Assigned:
Status: Not a bug Package: XML related
PHP Version: 4.0.6 OS: Win98SE/Linux
Private report: No CVE-ID: None
 [2001-08-25 09:41 UTC] slowbyte at hot dot ee
When you use XML methods in a script, $this doesn't get set or is buggy. That means you can't use instance variables or methods of that object.

When you run the following script you only get SE,SE,EE,EE but the variables content and instancevar aren't set. The only solution I could think of is that $this points to a wrong object, not the object that the XML methods are called in.

Example script:
<?
class ContentParser
{
    var $parser, $content = "";
    var $instancevar;


    function ContentParser()
    {
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, &$this);
        xml_set_element_handler($this->parser, "start_element", "end_element");
        xml_set_character_data_handler($this->parser, "character_data");
        xml_set_processing_instruction_handler($this->parser, "processing_instruction");
    }

    function parse_text ($data)
    {
        return xml_parse ($this->parser, $data, true);
    }


    function add_content($text)
    {
        $this->content .= $text;
    }

    function processing_instruction($parser, $target, $data)
    {

    }

    function start_element($parser, $name, $attribs)
    {
        echo "SE<br>";
        $this->add_content("Start_element $name<br>");
        $this->instancevar = "I should be set!!!";
    }

    function end_element($parser, $name)
    {
        echo "EE<br>";
        $this->add_content("End element $name<br>");
    }

    function character_data($parser, $data)
    {
        $this->add_content($data);
    }
}

$c = new ContentParser();
$c->parse_text("<document><paragraph>Well this doesn't work. Bug!</paragraph></document>");

echo $c->content; // Zip. No bonus.
echo $c->instancevar;
?>

Am I doing something wrong or is this really a bug?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-25 09:47 UTC] jmoore@php.net
Ask on the PHP-GEneral mailling list, you are trying to use $this outside of a class so it does not work.

- James
 [2001-08-25 09:47 UTC] slowbyte at hot dot ee
I noticed this bug is reported in several ways here (I didn't see these before posting, the 10 bug on 1 page limit, oops :). I hope this demonstrates this bug is not yet solved, and very annoying. (The script should also be clearer than others)

 [2001-08-25 09:49 UTC] jmoore@php.net
I should have been clearer this ISNT a bug, its an error in your programming, ask on the php-general list.

- James
 [2001-08-25 09:49 UTC] slowbyte at hot dot ee
$this outside a class? All references to $this are inside the class methods.
 [2001-09-01 14:22 UTC] sander@php.net
Just ask it! I'm sure there's someone on the PHP-GENERAL list who can explain it to you.
If you do not get a decent answer, reopen this report.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 21:01:23 2020 UTC