Detecting Function Calls
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Detecting Function Calls
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Detecting Function Calls
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.
(#10850)
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
That is what I am afraid of.arborint wrote:You probably would have to write a custom extension to create this functionality.
Re: Detecting Function Calls
I haven't got a PHP 5.3+ server at hand right now, but does this work?
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 
Code: Select all
<?php
namespace Dummy;
function strlen( $s )
{
print('...a few extra features...');
return \strlen($s);
}
///////////////////////////////
print(strlen('hello'));
?>- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
Hmm. Ok. That is quite interesting. Maybe I could come up with something... or not...Apollo wrote:I haven't got a PHP 5.3+ server at hand right now, but does this work?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 somethingCode: Select all
<?php namespace Dummy; function strlen( $s ) { print('...a few extra features...'); return \strlen($s); } /////////////////////////////// print(strlen('hello')); ?>
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
Not sure, but perhaps something like this?
I guess the code in someOtherFile.php is now automatically inside the Dummy namespace... 
Code: Select all
<?php
namespace Dummy {
function strlen( $s ) {
print('...a few extra features...');
return \strlen($s);
}
include('someOtherFile.php');
}
?>- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
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
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.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).
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
Thank you, I will definitely check it out.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.
Re: Detecting Function Calls
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
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
I want something implicit. I want to disable the use of a few functions by all means.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
Re: Detecting Function Calls
http://www.php.net/manual/en/ini.sect.s ... -functionskaisellgren wrote: disable the use of a few functions by all means.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
The only problem is that it is not going to work on many servers and I have no access to it from PHP:josh wrote:http://www.php.net/manual/en/ini.sect.s ... -functionskaisellgren wrote: disable the use of a few functions by all means.
This directive must be set in php.ini For example, you cannot set this in httpd.conf.
Re: Detecting Function Calls
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
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Detecting Function Calls
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.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 think I found my solution: no solution.