| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 08:00:01 2025 UTC | 
<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.netOn 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(); ?>