Page 1 of 2
Detecting Function Calls
Posted: Thu Apr 02, 2009 12:54 pm
by kaisellgren
Hi everyone,
I would love to be able to detect function calls in my application. This is, if someone calls a function, say, str_replace(), I would like to trigger a few things to happen along with it. Unfortunately, I am afraid this is not possible, but I was wondering if someone could possibly now a hack through reflection class or some other way to do it. I would much appreciate any solutions.
Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 2:02 pm
by Christopher
I have never heard of any callback that you can install for library functions. It may exist though. Have you checked the manual? You probably would have to write a custom extension to create this functionality.
Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 2:09 pm
by kaisellgren
arborint wrote:You probably would have to write a custom extension to create this functionality.
That is what I am afraid of.
Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 2:43 pm
by Apollo
I haven't got a PHP 5.3+ server at hand right now, but does this work?
Code: Select all
<?php
namespace Dummy;
function strlen( $s )
{
print('...a few extra features...');
return \strlen($s);
}
///////////////////////////////
print(strlen('hello'));
?>
If yes, perhaps you might enforce other php scripts to use this by redirecting everything to this script with .htaccess, specifying the original uri on the url, and the hook-strlen-script includes the original php after doing its namespace thing. Or something

Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 3:18 pm
by kaisellgren
Apollo wrote:I haven't got a PHP 5.3+ server at hand right now, but does this work?
Code: Select all
<?php
namespace Dummy;
function strlen( $s )
{
print('...a few extra features...');
return \strlen($s);
}
///////////////////////////////
print(strlen('hello'));
?>
If yes, perhaps you might enforce other php scripts to use this by redirecting everything to this script with .htaccess, specifying the original uri on the url, and the hook-strlen-script includes the original php after doing its namespace thing. Or something

Hmm. Ok. That is quite interesting. Maybe I could come up with something... or not...
I'm not exactly sure how you would go for forcing the use of Dummy:strlen() instead of just strlen() ? if that is somehow possible, then I would like to know how!
Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 3:44 pm
by Apollo
Not sure, but perhaps something like this?
Code: Select all
<?php
namespace Dummy {
function strlen( $s ) {
print('...a few extra features...');
return \strlen($s);
}
include('someOtherFile.php');
}
?>
I guess the code in someOtherFile.php is now automatically inside the Dummy namespace...

Re: Detecting Function Calls
Posted: Thu Apr 02, 2009 4:25 pm
by kaisellgren
So, basically, if I wrap my entire script within a namespace, I could achieve that? The problem is now that I can't have PHP 5.3 as a minimum requirement especially since it is not even out yet (final).
Re: Detecting Function Calls
Posted: Fri Apr 03, 2009 12:17 pm
by webaddict
kaisellgren wrote:So, basically, if I wrap my entire script within a namespace, I could achieve that? The problem is now that I can't have PHP 5.3 as a minimum requirement especially since it is not even out yet (final).
True, it isn't stable yet, it's still in RC1. However, there is a way to achieve this in PHP < 5.3, but it doesn't work on the vast majority of the servers. If that is no issue whatsoever, you might want to check out
php.net/runkit, it's a library you can use to change the behaviour of functions in PHP.
Re: Detecting Function Calls
Posted: Fri Apr 03, 2009 12:26 pm
by kaisellgren
webaddict wrote:you might want to check out
php.net/runkit, it's a library you can use to change the behaviour of functions in PHP.
Thank you, I will definitely check it out.
Re: Detecting Function Calls
Posted: Mon Apr 06, 2009 4:01 pm
by josh
Instead of using functions, abstract those things as method objects, then you can wrap those objects ( implementing the decorator pattern ) to add functionality. You'd have to modify the code to use objects but that would be the next best option
Re: Detecting Function Calls
Posted: Mon Apr 06, 2009 4:10 pm
by kaisellgren
josh wrote:Instead of using functions, abstract those things as method objects, then you can wrap those objects ( implementing the decorator pattern ) to add functionality. You'd have to modify the code to use objects but that would be the next best option
I want something implicit. I want to disable the use of a few functions by all means.
Re: Detecting Function Calls
Posted: Mon Apr 06, 2009 11:13 pm
by josh
kaisellgren wrote: disable the use of a few functions by all means.
http://www.php.net/manual/en/ini.sect.s ... -functions
Re: Detecting Function Calls
Posted: Tue Apr 07, 2009 6:20 am
by kaisellgren
The only problem is that it is not going to work on many servers and I have no access to it from PHP:
This directive must be set in php.ini For example, you cannot set this in httpd.conf.
Re: Detecting Function Calls
Posted: Tue Apr 07, 2009 9:46 am
by josh
I'm not following you here, anyways its a cleaner solution then moding the PHP source / writing an extension. Unfortunately since PHP is interpreted there is no linking, you have to switch out extensions. SInce you just want to _disable_ the functions and there is already a directive I'd recommend using that. I don't see how editing httpd.conf would be any more scalable than doing it in php.ini, they are both .ini format flat files
Re: Detecting Function Calls
Posted: Tue Apr 07, 2009 9:51 am
by kaisellgren
josh wrote:I'm not following you here, anyways its a cleaner solution then moding the PHP source / writing an extension. Unfortunately since PHP is interpreted there is no linking, you have to switch out extensions. SInce you just want to _disable_ the functions and there is already a directive I'd recommend using that. I don't see how editing httpd.conf would be any more scalable than doing it in php.ini, they are both .ini format flat files
I have no access to other people's servers, so, I need to do this PHP-side. Following? And editing ini settings does not work for safe mode settings, obviously. Writing an extension could neither work, since I would need to ask a person on a shared hosting to enable it, which does not sound that great to me. I could use dl(), but I would rather give up on this.
I think I found my solution: no solution.
