# How should I call a PHP function

- Thread: https://externals.io/message/76625
- Original archive: https://news-web.php.net/php.internals/76625
- Started by Aaron Lewis on 2014-08-18T02:19:31+00:00
- Messages: 11
- 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

---

## #76625 — Aaron Lewis — 2014-08-18T02:19:31+00:00

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

Hi,

I'm trying to call a function inside a module, a PHP_FUNCTION.

Other than zend_eval_stringl, what's the direct way to do it?


-- 
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #76626 — Wei Dai — 2014-08-18T02:37:02+00:00

URL: https://externals.io/message/76625#76626 · In reply to: #76625

Hi,
> Hi,
> 
> I'm trying to call a function inside a module, a PHP_FUNCTION.
> 
> Other than zend_eval_stringl, what's the direct way to do it?
Please refer to call_user_function and call_user_function_ex. 
> 
> 
> -- 
> Best Regards,
> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
> Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
> 
> -- 
> 
> 
> 
> 

cheers, 
Wei Dai

---

## #76638 — Chris Wright — 2014-08-18T09:36:55+00:00

URL: https://externals.io/message/76625#76638 · In reply to: #76626

> Hi,
> > […]
> Please refer to call_user_function and call_user_function_ex.

...and along these lines, when you need to do something that is done
in other parts of the codebase, take a look at how those other parts
do them - searching lxr for "call_user_func" led me straight to
http://lxr.php.net/xref/PHP_5_6/ext/standard/basic_functions.c#4738
;-)

---

## #76641 — johannes@schlueters.de — 2014-08-18T10:07:46+00:00

URL: https://externals.io/message/76625#76641 · In reply to: #76626

> Hi,
> > […]
> Please refer to call_user_function and call_user_function_ex. 

See also the set of zend_call_method_with_[0-4]_params macros in
zend_interfaces.h. Those allow to cache the function pointer (or use the
one provided via "f" option by zend_parse_parameters) and therefore
saving repeated lookups.

But general comment: Do you really want to call a user function? - This
might be the case when using callbacks, typically when this question
pops up the aim is to call some PHP provided function, then often there
is a better C level API ... going via PHP function is cumbersome and
"slow".

johannes

---

## #76644 — Derick Rethans — 2014-08-18T10:14:55+00:00

URL: https://externals.io/message/76625#76644 · In reply to: #76626

> > […]
>
> Please refer to call_user_function and call_user_function_ex.

Actually, you need to be careful with this. If you are calling another 
PHP function in the same extension, do *not* use call_user_function. 
Instead, create a common C function that you can call from whereever you 
need to. call_user_function(_ex) is not fast.

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #76675 — Wei Dai — 2014-08-19T01:39:14+00:00

URL: https://externals.io/message/76625#76675 · In reply to: #76644

Hi Derick,  
> 
>  
> > […]
>  
> Actually, you need to be careful with this. If you are calling another  
> PHP function in the same extension, do *not* use call_user_function.  
> Instead, create a common C function that you can call from whereever you  
> need to. call_user_function(_ex) is not fast.
>  
i agree.  

here is a scenario:

i was wrote an extension, and i need to call some PHP function like “strtr (http://lxr.php.net/s?defs=strtr&project=PHP_5_4)”  “*trim"  or a function in  
another extension but it doesn’t have a common C function. However, i don’t want to copy the implement
code into my extension.

so, i have two options:

1. copy the duplicate code into my extension, It can be fast in this way, but the code is not beautiful.
2. call call_user_function(_ex) , it’s slower than the first option, but the code is more simple and beautiful than the first option.


cheers,
Wei Dai

---

## #76685 — Derick Rethans — 2014-08-19T08:55:20+00:00

URL: https://externals.io/message/76625#76685 · In reply to: #76675

> > […]
> i agree.  
> 
> here is a scenario:
> 
> i was wrote an extension, and i need to call some PHP function like 
> “strtr (http://lxr.php.net/s?defs=strtr&project=PHP_5_4)” “*trim"  or 
> a function in another extension but it doesn’t have a common C 
> function. However, i don’t want to copy the implement code into my 
> extension.
> 
> so, i have two options:
> 
> 1. copy the duplicate code into my extension, It can be fast in this 
> way, but the code is not beautiful.
>
> 2. call call_user_function(_ex) , it’s slower than the first option, 
> but the code is more simple and beautiful than the first option.

or:

3. refactor the functionality in PHP so that trim or others *can* be 
used in extensions.

I would not pick option 2, especially not if you're going to call this 
function a lot. Calling a PHP function directly involves a lot more 
overhead than just calling a C-function. 


cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #76686 — Pierre Joye — 2014-08-19T09:03:57+00:00

URL: https://externals.io/message/76625#76686 · In reply to: #76685

> 
>
> > […]

> > […]
>
> or:
>
> 3. refactor the functionality in PHP so that trim or others *can* be
> used in extensions.
>

Best option, while not possible for existing releases, a good example (trim):

http://lxr.php.net/xref/phpng/ext/standard/string.c#784

Cheers,
-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #76687 — Wei Dai — 2014-08-19T09:11:54+00:00

URL: https://externals.io/message/76625#76687 · In reply to: #76686

> 
> > […]
>  
> > […]
>  
>  
> Best option, while not possible for existing releases, a good example (trim):
>  
> http://lxr.php.net/xref/phpng/ext/standard/string.c#784
>  
trim is just an example, we can call it 'x'.
x is a php function and it doesn't have a common C api.

---

## #76690 — Pierre Joye — 2014-08-19T09:21:06+00:00

URL: https://externals.io/message/76625#76690 · In reply to: #76687

>
> 
>
> 
>
> 
>
>
> so, i have two options:
>
> 1. copy the duplicate code into my extension, It can be fast in this
> way, but the code is not beautiful.
>
> 2. call call_user_function(_ex) , it’s slower than the first option,
> but the code is more simple and beautiful than the first option.
>
>
> or:
>
> 3. refactor the functionality in PHP so that trim or others *can* be
> used in extensions.
>
>
> Best option, while not possible for existing releases, a good example
> (trim):
>
> http://lxr.php.net/xref/phpng/ext/standard/string.c#784
>
> trim is just an example, we can call it 'x'.
> x is a php function and it doesn't have a common C api.

Yes, just pointing you at how it could be done, for functions not
exposed as a C API.


-- 
Pierre

@pierrejoye | http://www.libgd.org

---

## #76696 — Wei Dai — 2014-08-19T09:44:12+00:00

URL: https://externals.io/message/76625#76696 · In reply to: #76685

> 
>  
> > […]
>  
>  
> or:
>  
> 3. refactor the functionality in PHP so that trim or others *can* be  
> used in extensions.
>  
> I would not pick option 2, especially not if you're going to call this  
> function a lot. Calling a PHP function directly involves a lot more  
> overhead than just calling a C-function.  
>  
you are right.
the reason to write php extension, because we can get performance improvement.

cheer,
Wei Dai
