views:

35

answers:

3

OK so is there a way in php do track function calls such as

function Tracker($name,$returnedValue,$file,$line)
{
   echo $name . '() was called and returned a ' . typeof(returnedValue);
}

function test(){}

test();

The reason for this is to send a custom framework data type back so another example would be

$resource = fopen('php://stdin'); //This would return an instance of (Object)Resource.

if($resource->type == 'fopen')
{
    //Code
}

I have never seen anyway to do this but does anyone know if it is possible ?

+1  A: 

Not really. Xdebug is able to log function calls though: http://xdebug.org/docs/execution_trace

Daniel Egeberg
Its not logging, i want to be able to return a custom datatype from php, so if i called `trim()` usually it would return a plain string, where as i want it to return custom object such as `String`.
RobertPitt
@RobertPitt: Well, that's not possible. You'll have to modify the PHP core if you wish to do that.
Daniel Egeberg
Yea i had a feeling it would resort to that :( but as it a Public project i cant be asking users to modify there core too much, and I have a very strict read map in my mind :( worth a try I suppose.
RobertPitt
A: 

Maybe you want something like Observer pattern? http://en.wikipedia.org/wiki/Observer_pattern

Mchl
Yea i already thought of the Dispatcher but I don't have access to php's functions to alert notifications.
RobertPitt
+2  A: 

It's not possible to do this using just PHP, a debugger might help, however you could wrap the function:

function wrapper()
{
    $args=func_get_args();
    $function=array_shift($args);
    $returned=call_user_func_array($function, $args);
    print "$function (" . var_export($args, true) . ") = " 
         . var_export($returned, true) . "\n";
    return $returned;
}

$value=wrapper('test_fn', 1 ,2 ,3, 'something');
$value=wrapper('mysql_connect');

I don't understand your explanation of what you are trying to achieve here.

C.

symcbean
hmmm, possible `new Resource('fopen','php://stdin',true);` nice idea, ill see if i can make anything of it.
RobertPitt
what im trying to do is add a data-type object to the regular data-types, so instead of `$variable = "string";` i would do `$variable = new String("string contents")` and then teh variable would have methods such as section to get a portion of the string, trim and scape etc. this would be used for strings,arrays,stdclass'es,resources etc. and the reason i want to track function calls is so i dont have to use wrappers for functions that create data-types such as Fopen, and Fread, where Fread would return a String Object.
RobertPitt