# intercepting function calls

- Thread: https://externals.io/message/14737
- Original archive: https://news-web.php.net/php.internals/14737
- Started by sumoraigabe-php@yahoo.com on 2005-02-09T05:57:45+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #14737 — sumoraigabe-php@yahoo.com — 2005-02-09T05:57:45+00:00

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

Is it possible to intercept a function call (user
space or built-in) in the Zend Engine and execute user
space code before/after the function call?

=====

---

## #14751 — Sara Golemon — 2005-02-09T15:57:04+00:00

URL: https://externals.io/message/14737#14751 · In reply to: #14737

> Is it possible to intercept a function call (user
> space or built-in) in the Zend Engine and execute user
> space code before/after the function call?
>
Yes, you can do this by way of a Zend extension (not a PHP extension mind 
you).

The parts of Zend/zend_extensions.h you'll want to pay attention to are:

typedef void (*fcall_begin_handler_func_t)(zend_op_array *op_array);
typedef void (*fcall_end_handler_func_t)(zend_op_array *op_array);

struct _zend_extension {
    ...
    fcall_begin_handler_func_t fcall_begin_handler;
    fcall_end_handler_func_t fcall_end_handler;
    ...
};

-Sara

---

## #14752 — George Schlossnagle — 2005-02-09T16:01:22+00:00

URL: https://externals.io/message/14737#14752 · In reply to: #14751

> > […]
> Yes, you can do this by way of a Zend extension (not a PHP extension 
> mind
> you).
>
> The parts of Zend/zend_extensions.h you'll want to pay attention to 
> are:

You can also do this by directly wrapping zend_execute.  This is the 
preferred method if you're writing a profiler or such.

George

---

## #14764 — Sebastian Bergmann — 2005-02-09T17:50:09+00:00

URL: https://externals.io/message/14737#14764 · In reply to: #14751

Sara Golemon wrote:
> typedef void (*fcall_begin_handler_func_t)(zend_op_array *op_array);
> typedef void (*fcall_end_handler_func_t)(zend_op_array *op_array);
> 
> struct _zend_extension {
>     ...
>     fcall_begin_handler_func_t fcall_begin_handler;
>     fcall_end_handler_func_t fcall_end_handler;
>     ...
> };

 What about patching __call() so that it is always called?

-- 
Sebastian Bergmann                      http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

---

## #14767 — Sara Golemon — 2005-02-09T18:05:44+00:00

URL: https://externals.io/message/14737#14767 · In reply to: #14764

> > […]
>
> What about patching __call() so that it is always called?
>
I hesitate to solve a problem via engine patch if it's at all possible to do 
within an extension.  I havn't entirely mapped it out, but it certainly 
looks like some magic methods could be hooked out using a zend_extension: 
function __pre($funcname, &$args) {} and function __post($funcname, &$args, 
&$retval) for procedural calls and matching object methods for class calls. 
No time to look deeper myself, but curious to see what others come up with.

-Sara
