php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24644 Fatal error because of private method
Submitted: 2003-07-14 08:27 UTC Modified: 2003-11-28 09:29 UTC
From: Bertrand dot Willm at laposte dot net Assigned:
Status: Closed Package: XML related
PHP Version: 5.0.0b2-dev OS: Windows XP
Private report: No CVE-ID: None
 [2003-07-14 08:27 UTC] Bertrand dot Willm at laposte dot net
Description:
------------
I make a class to parse XML.
The element handler functions are private methods.
I tried to call a private method from the StartElement handler.
There is a fatal error.
This should not be the case as all these functions are declared in the same class.
In a class, private function should have access to the other private functions of the class.


Reproduce code:
---------------
class CBaseClass {
   var $name;
   function Parse() {
      $parser = xml_parser_create();
      xml_set_object($parser, $this);
      xml_set_element_handler($parser, 'StartElement', 'EndElement');
      xml_parse($parser, '<xml><node/></xml>', true);
      xml_parser_free($parser);
   }
   
   private function StartElement() {
      $this->PrivateFunction();
   }

   private function EndElement() {
   }

   private function PrivateFunction() {
   }
}

class CExtClass extends CBaseClass {
}

$ExtObject = new CExtClass();
$ExtObject->Parse();


Expected result:
----------------
no error

Actual result:
--------------
Fatal error: Call to private method cbaseclass::PrivateFunction() from context 'cextclass' in c:\sitesweb\www\test.php5 on line 14

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-14 14:26 UTC] helly@php.net
The only interesting part here is that the error message should say that private method StartElement() was called.

The problem is that by the call to xml_parse() the connection to the instance gets lost. you should use array($this,'<method_name>') instead. Try that please.
 [2003-07-16 01:15 UTC] Bertrand dot Willm at laposte dot net
I replace the following line :
xml_set_element_handler($parser, 'StartElement', 'EndElement');
with :
xml_set_element_handler($parser, array(&$this, 'StartElement'), array(&$this, 'EndElement'));
but PHP crashes.
 [2003-07-16 02:54 UTC] helly@php.net
Not good, but anyway please try without & for $this: 
array($this...)
 [2003-07-22 12:52 UTC] Bertrand dot Willm at laposte dot net
There is not difference with or without the &.
 [2003-07-29 09:12 UTC] sfox@php.net
Just a note to say I've now tested this with sane data and using helly@php.net's array.  It works fine if you call the base class directly.  I moved the $this->privateFunction() call to the EndElement() function to check what's happening; when called via the extended class, the entire thing works except that privateFunction() throws its (expected) error.

Basically the xml parser handlers are bypassing the ppp checks.

Leaving this bug open as unsure whether this is intended behaviour.




 [2003-08-07 16:07 UTC] erik at erikcentral dot net
<snip>
   function CreateXMLParser() {
      $this->parser = xml_parser_create("ISO-8859-1");
      $new_ref = $this;
      xml_set_object($this->parser, $new_ref);
      xml_set_element_handler($this->parser,array($this,"myOpenElement"),array($this,"myCloseElement"));
      xml_set_character_data_handler($this->parser,array($this,"myCharData"));
   }
<snip>


works on Linux phpV5.0.0b1
Thanks helly@php.net
 [2003-08-07 16:17 UTC] erik at erikcentral dot net
Correction.
The class works from the CLI but causes apache to segfault. Linux phpV5.0.0b1
 [2003-08-19 09:53 UTC] Bertrand dot Willm at laposte dot net
On version PHP 5.0.0b2-dev there is no crash, but can't access to private member.
The context is not the good one should be cbaseclass whereas it is cextclass.
This is the error:
Fatal error: Call to private method cbaseclass::PrivateFunction() from context 'cextclass' in c:\sitesweb\www\test.php5 on line 16
This is the code I used:
<?php
class CBaseClass {
   var $name;
   function Parse() {
      $parser = xml_parser_create();
      xml_set_object($parser, $this);
      xml_set_element_handler($parser, array($this, 'StartElement'), array($this, 'EndElement'));
      $str = '<xml><node/></xml>';
      xml_parse($parser, $str, true);
      xml_parser_free($parser);
   }
   
   private function StartElement($parser, $name) {
      echo $name, '<br>';
      $this->PrivateFunction();
   }

   private function EndElement() {
   }

   private function PrivateFunction() {
   }
}

class CExtClass extends CBaseClass {
}

$ExtObject = new CExtClass();
$ExtObject->Parse();
?>
 [2003-11-28 09:29 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 09:01:27 2024 UTC