Re: Object method as handler functions in PHP4/Zend
| From: | Manuel Lemos | Date: | Wed, 30 Jun 1999 02:30:24 +0000 |
| Subject: | Re: Object method as handler functions in PHP4/Zend | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-7861@lists.php.net to get a copy of this message | ||
Hello Zeev,
On 30-Jun-99 05:58:08, you wrote:
>At 03:39 30/06/99 , Manuel Lemos wrote:
>>Hello Zeev,
>>
>>On 30-Jun-99 05:24:09, you wrote:
>>
>> >> >You can call an object method from outside an object in PHP 4.0,
>> using the
>> >> >notation of classname::method().
>> >>
>> >> >Right now, only static strings are accepted for both the class name and
>> >> >the method name, but we may make them dynamic so that you can this
>> >> >notation indirectly.
>> >>
>> >>That's better, but I don't know if that solves the reentrancy problem.
>> >>I'm not sure what you mean by dynamic, but I hope that means you can pass
>> >>a string as name of a callback function and the function is called in the
>> >>context of a user defined object of a given class. Thatr would mean that
>> >>the $this references in the method function reference the functions of
>> >>the class and the variables of the user defined object.
>>
>> >No, it doesn't mean that (I was talking about methods that don't really
>> >have object contexts). Frankly, I'm not really sure what you're after.
>> >You can pass that function a 'pointer' to an object, and a method name,
>> >and call $object->$method(). I think you could do that in PHP 3.0, but if
>> >you couldn't, you can do it using PHP 4.0.
>>
>>Well, to present an example that you may understand right away, think of a
>>class that is used to parse a specific XML format. Ideally you would use
>>the class methods as the parser's XML element and data handlers and use
>>that variables of the object to store some format specific contextual
>>information.
>>
>>In PHP 3 you can't use the class method functions as handlers. From what
>>you say, that is possible in PHP 4, but you still can't store the object
>>variables to store parsing contextual information.
>>
>>What I am after is some way to pass to the XML functions that set the
>>handler functions, some reference that would let the XML parser call the
>>class methods in the context of a given object. Was this clear enough?
>Some pseudo-code examples of what you're after would be nice.
What follows is the skelleton of a typical XML parser class:
<?
class xml_parser_class
{
var $xml_parser=0;
var $error_number=0;
var $error_line;
var $error_column;
var $error_byte_index;
Function Setup()
{
if(!($this->xml_parser=xml_parser_create()))
return(0);
xml_set_element_handler($this->xml_parser,ConvertMethodToObjectFunction($this,"StartElementHandler"),ConvertMethodToObjectFunction($this,"EndElementHandler"));
xml_set_character_data_handler($this->xml_parser,ConvertMethodToObjectFunction($this,"CharacterDataHandler"));
return(1);
}
Function Parse($data,$end_of_data)
{
return(xml_parse($this->xml_parser,$data,$end_of_data));
}
Function SetError($error)
{
$this->error=$error;
$this->error_line=xml_get_current_line_number($this->xml_parser);
$this->error_column=xml_get_current_column_number($this->xml_parser);
$this->error_byte_index=xml_get_current_byte_index($this->xml_parser);
}
Function StartElementHandler($name,&$attrs)
{
test consistency
if($some_error)
$this->SetError($some_error);
}
Function EndElementHandler($name)
{
test consistency
if($some_error)
$this->SetError($some_error);
}
Function CharacterDataHandler($data)
{
test consistency
if($some_error)
$this->SetError($some_error);
}
};
?>
>If what you're after is a transparent way of passing a method&object
>combination in place of a function name, and make $funcname() transparently
Right.
>call object->method(), no, there's no way to do it (and I think it's safe
>to say there won't be one either).
But why not?
If you ever try to wrap a XML parser in a class you'll see the only way to
work arround the current limitations of PHP is to mess a lot global
variables.
Not very pretty, neither not prone to name collision, which is not a good
thing specially because from PHP 4 on you will be able to distribute you
PHP code in compiled form without letting people tweak global names that
collide.
>But, if you know you're expecting an object+method/property combination,
>there's nothing that stops you from calling any of these methods in the
>context of the object, and access any of the object's properties, from
>wherever you have the object handler and method/property names. You can do
>it from both PHP code and C code.
As you see from the XML call back handler example, I don't have a way to do
that because XML parser expect a function name, not a class method , even
less a method of specific object.
Regards,
Manuel Lemos
Web Programming Components using PHP Classes.
Look at: http://phpclasses.UpperDesign.com/
--
E-mail: mlemos@acm.org
URL: http://www.mlemos.e-na.net/
PGP key: finger://mlemos@zeus.ci.ua.pt
--