Detecting Function Calls

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Detecting Function Calls

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Detecting Function Calls

Post 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.
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post by kaisellgren »

arborint wrote:You probably would have to write a custom extension to create this functionality.
That is what I am afraid of.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Detecting Function Calls

Post 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 :)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post 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!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Detecting Function Calls

Post 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... :?:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post 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).
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Re: Detecting Function Calls

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Detecting Function Calls

Post 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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Detecting Function Calls

Post by josh »

kaisellgren wrote: disable the use of a few functions by all means.
http://www.php.net/manual/en/ini.sect.s ... -functions
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post by kaisellgren »

josh wrote:
kaisellgren wrote: disable the use of a few functions by all means.
http://www.php.net/manual/en/ini.sect.s ... -functions
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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Detecting Function Calls

Post 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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Detecting Function Calls

Post 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. :(
Post Reply