PHP know thy self.
February 18, 2005 1:24 PM   Subscribe

Is it possible in PHP to monitor if a function is called?

For example:
if I have function foo() and I call it, is there something in PHP that will go "Hey, this is foo(), and I've been used/called!"? A trigger of sorts.
posted by grefo to Computers & Internet (9 answers total)
 
define something within the function foo() and then check that definition outside of the function ...
posted by bryanzera at 1:30 PM on February 18, 2005


grefo -- what are you trying to do?

You can always call a 2nd function, i.e. put "wascalled('foo');" inside of foo().
posted by SpecialK at 1:38 PM on February 18, 2005


is this a function you're writing yourself, or one that's already been written. If its your own function, it could just log itself.

Otherwise you could try hacking the intrepreter.
posted by delmoi at 1:57 PM on February 18, 2005


You need a profiler, like this one.
It will tell you what functions get called, how many times each funciton is called, and how long they take to run.

Or you could use some Aspect Oriented Programming (AOP). I have no idea if their is any AOP library worth using in PHP, but I found an article about it.
posted by gus at 2:14 PM on February 18, 2005


Here's what I would like to do:

Anytime the function foo() is used, only then do I want to instantiate an instance of my SOAP object, otherwise don't create an instance. Rather than doing some if isset or !empty, this "monitor" is just waiting for the function to fire.

The functions are already written, but it's no big thing to rewrite if necessary.

I'm just thinking it would be neat to have a function monitor, but not sure if it's possible or how elegant a hacked solution could be. Ultimately, I think I will resort to the "if" statement.

Is that any clearer?
posted by grefo at 2:51 PM on February 18, 2005


grefo: Err, just insantiate the SOAP object in foo(), then.

If you make a global reference to a SOAP object $obj inside foo(), it should work globally as well.
posted by xmutex at 3:46 PM on February 18, 2005


Encapsulate the SOAP object in another object that contains the object and the foo() function. This avoids using global variables, which is generally frowned upon.

< ?br>
// Your SOAP class
include_once( "soap.inc.php" );

// New wrapper class
class myObj {

var $_soap; // Soap object

// Function constructor
function myObj ( ) {
print "Created object\n";

// We start with no SOAP object.
$this_>soap = false;
}

// Foo function , to create soap object
function foo ( ) {

// Dont' need to create one if already exists
if ( !( $this->_soap ) ) {
print "Creating SOAP object\n";
$this->_soap = new SOAPObject();
}

}

// Do something with the SOAP object
function soap_do ( $args ) {
$this->_soap->do( $args );
}

}

// Example usage:

// Create object
$object = new myObj();

// Call foo() function to create SOAP object
$object->foo();

// Do some SOAP
$object->soap_do( array( "one" => 1, "two" => 2 ) );

?>
posted by gaby at 4:53 PM on February 18, 2005


While this isn't a "hands-off" way to test function calls, you could organize your function call(s) in the following manner and then test for them. (Your functions would need to have return values.)

$test = whatever_func($data); // calls the functon - stores return val in $test var

if ($test) { whatever; }
else { whatever; }
posted by Hankins at 5:21 PM on February 18, 2005


I nicer way to do it is using listeners.

set up a global array like

$foo_listeners = array();


then add functions you want called to it

function observer() {
   echo "observed foo being called";
}

array_push($foo_listeners, observer);


then in the foo function

function foo() {
    global $foo_listeners;
    foreach ($foo_listeners as $listener) {
         $listener();
    }

    // do stuff
}

posted by drscroogemcduck at 9:43 PM on February 18, 2005


« Older Mashed Potatoes   |   Recommendations for voice recognition software? Newer »
This thread is closed to new comments.