Re: [PECL-DEV] Callling callbacks

[email protected] (Johannes Schlüter) Mon, 26 Apr 2010 12:38:37 +0200
Newsgroups php.pecl.dev
Message-ID <1272278317.28165.31.camel@guybrush>
On Thu, 2010-04-22 at 20:21 +0100, "Gustavo Lopes" wrote:
> On Wed, 21 Apr 2010 21:23:10 +0100, Stanislav Malyshev <[email protected]>  
> wrote:
> 
> > Hi!
> >
> >> What is the best way to have a function accept an arbitrary callback (be
> >> it a string, array or closure in case of PHP 5.3) that is compatible
> >> with PHP 5.2 and 5.3?
> >
> > In 5.3+, you should use "f" parameter character. Unfortunately, in 5.2  
> > there was none, so you'd probably have to have different code in 5.2  
> > branch.
> >
> 
> Apparently, the "f" parameters character also exists in 5.2, it simply  
> wasn't used in the standard extension.

Just as a quick note: It was introduced after 5.2.0, I think, don't
exactly remember. All recent versions should support it though.

> However, I'm starting to think I shouldn't use the "f" parameter. I don't  
> want to call the callback immediately (before returning) so my initial  
> approach was to store zend_fcall_info (and its function_name zval) and  
> zend_fcall_info_cache. Well, in 5.2 I couldn't get it to work. If I did  
> something like
> 
> func(array(new A(), 'method'))
> 
> the object of type A would be destroyed by the time the callback was  
> called and the data in zend_fcall_info(_cache) would be outdated (in  
> particular fic(_cache).object_pp would point to nowhere). I suppose you  
> could fiddle with the refcounts, but it made me think that storing  
> zend_fcall_info(_cache) was not the correct approach.
> 
> So which is it? Accepting the callback as a zval*, increment its refcount  
> and store it and then creating zend_fcall_info(_cache) on each callback  
> execution using zend_fcall_info_init?

See
http://github.com/johannes/php-test-helpers/blob/master/test_helpers.c#L218 where I'm registering a callback and http://github.com/johannes/php-test-helpers/blob/master/test_helpers.c#L107 where I call it.
http://github.com/johannes/php-test-helpers/blob/master/test_helpers.c#L74 is the cleanup.

Hope that helps,
johannes