YANQ: Y(et) A(nother) N(ewbie) Q(ueries)

From: Date: Fri, 17 Nov 2000 03:49:36 +0000
Subject: YANQ: Y(et) A(nother) N(ewbie) Q(ueries)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-25862@lists.php.net to get a copy of this message
Hi everyone. PHP Version 4.0.2 Two things that I can't figure out. Short versions (long versions follow below): 1. Does anyone have an idea how I can talk PHP into finding a user-supplied global function from within an xml parser's handler that has been xml_set_object'd using the object's variable that is storing a string of the function's name? (more details below). 2. Why does globally reading an object's variable not report the value it was set to in the object (using an xml handler function that is called from within a set_object'd xml parser). And setting the variable to a new value by a global function is not recognized from within the variable? (See below for details and examle code). 1. I have an object. class some_object { The object includes the variable var $some_func; The object constructor accepts a string that is a function name, or assigns a default function name to this variable: function some_object( $_some_func = "default_func") { and then assigns the value of $_some_func to the object's variable $some_func $this->some_func = $_some_func; In this object I am using the xml_parser. I've used xml_set_object to make the xml_parser aware of the object's context. I've registered handlers with the xml_parser. These handlers are implemented in the object . The handlers do get called. So far so good. One of the handlers I've registered with the xml_parser includes a function call to "default_func". Note that the function "default_func" is not defined in the object. It is defined outside of the object, in the same file (I don't think that being defined in the same file is relevant, is it?) ===>>> When I try calling the none-object function "default_func" indirectly using the notation $this->some_func( argument ); from within the handler I get the following error message: Fatal error: Call to undefined function: default_func Note: The function's name is assigned (as a string) to the variable in the object's constructor. xml_parse is called from within the object's parse function,: xml_object = new XML(); xml_object->parse(). According to the error message, PHP is looking for the global function default_func somewhere where it does not exist. I tried using the notation GLOBAL[$this->some_func]( argument ); but PHP refuses to even parse this expression. I guess it's not well-formed ... 2. I've worked around problem 1 by using a local f function in the hanlder: $f = $this->some_func; $f( $argument ); Lo and behold, PHP finds the function. So apparently it's not that PHP cannot detect the function from within the handler - as the error message suggests - there must be something else going on here. The workaround I'm using is overly verbose, a waste of keyboard activity, storage, and ram. There must be a more direct way of calling the function? At least the function is being called, which leads me to problem 2. The default_func function is found and evaluated. In the function I want to retrieve values that were stored in one of the object's variables, $some_var. Recall that default_func is global. Before the xml handler function ($this->handler_func()) calls $f( $argument), it first assigns a value to $this->some_var. I try accessing the variable $this->some_var using the notation $xml_object->some_var from with the user-supplied, none-object function default_func. The value of $xml_object->some_var is not being returned. I can set this value from within the function, and after setting it I can also retrieve from the same function. However, the handler function, which access $xml_object->some_var as $this->some_var retrieves the value it stored both before and after default_func is called. This looks as though $xml_object->some_var and $this->some_var are two different instances. But I only created once instance of my XML object. What's going on here? xml_object = new XML(); xml_object->parse(); class XML { var $some_var; function handler ( $parser, $arg_1, $arg_2 ) { $this->some_var = $arg_1; $f = ... (see above); echo($this->some_var"); $f( $arg ); echo("$this->some_var"); // results in the same value being displayed as previous echo. However: } } // end XML object funtion default_func ( $arg ) { echo("$xml_object->some_var"); // displays nothing $xml_object->some_var = "This the function default_func."; echo("$xml_object->some_var"); // displays the string I just assigned. Whereas xml_object's handler function // continues to display the value of $arg_1 that IMHO should have been overwritten by the assignment in this function. } Any ideas? TIA, Elan

« previous php.general (#25862) next »