# Recommended way to create a zval that serves as a callback in an extension

- Thread: https://externals.io/message/61234
- Original archive: https://news-web.php.net/php.internals/61234
- Started by Adam Jon Richardson on 2012-07-14T21:00:35+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61234 — Adam Jon Richardson — 2012-07-14T21:00:35+00:00

URL: https://externals.io/message/61234#61234

Hi,

I'm calling the function php_pcre_replace() in my extension:
http://lxr.php.net/xref/PHP_5_4/ext/pcre/php_pcre.c#952

I would like to use a callback for the replace_val, but this will be
supplied by my extension. I'm wondering what the recommended way of
creating the replace_val zval callback would be in the extension.

Thank you for your patience and help.

Adam

---

## #61512 — Sara Golemon — 2012-07-19T18:49:40+00:00

URL: https://externals.io/message/61234#61512 · In reply to: #61234

Same as you would in PHP code.  Make an IS_STRING pointing at a global
function, or IS_ARRAY pointing at a class method, or an IS_OBJECT
supporting __invoke().  If you're asking how you can call a C function
directly via this callback: You can't.  You can can, however, make an
internal method to use as a callback, just bear in mind it'll be callable
by userspace as well.

-Sara


On Sat, Jul 14, 2012 at 2:00 PM, Adam Jon Richardson <adamjonr@gmail.com>wrote:

> Hi,
>
> I'm calling the function php_pcre_replace() in my extension:
> http://lxr.php.net/xref/PHP_5_4/ext/pcre/php_pcre.c#952
>
> I would like to use a callback for the replace_val, but this will be
> supplied by my extension. I'm wondering what the recommended way of
> creating the replace_val zval callback would be in the extension.
>
> Thank you for your patience and help.
>
> Adam

---

## #61572 — Adam Jon Richardson — 2012-07-20T16:43:54+00:00

URL: https://externals.io/message/61234#61572 · In reply to: #61512

> Same as you would in PHP code.  Make an IS_STRING pointing at a global
> function, or IS_ARRAY pointing at a class method, or an IS_OBJECT supporting
> __invoke().  If you're asking how you can call a C function directly via
> this callback: You can't.  You can can, however, make an internal method to
> use as a callback, just bear in mind it'll be callable by userspace as well.

Thank you very much for your explanation, Sara. I got my code working,
but I must confess I wasn't confident it was the preferred approach
and I wanted to make sure I was learning the recommended approach.
I've taken to heart the many examples in your book where you bring up
the most obvious/likely method to do something in the extension world
only to find out it's not the preferred/correct approach.

Thank you very much for the clarification.

Adam

P.S. - Your book has aged exceedingly well, and it's a great reference
for people like me trying to find their way around the internals :)
Nice writing style!
