views:

118

answers:

2

What are the 'costs' involved in calling an empty PHP function? The main purpose is for a CMS to trigger included module events at various points in its lifespan to handle things like security, access permissions, post processing, timing, http(s) and the like. Since there can be over a dozen modules called dozens of times each the total calls will reach the hundreds and potentially even the thousands at some point for each page load.

The initial module will basically look like the following code, which will be replaced by the object to handle whatever trigger event was fired and process the passed data:

function trigger ($trigger_name, $data=false)
{
    return false;
}

Will this be a problem eventually and should I instead work in a system of the modules pre-registering a trigger to cut down on the amount of pointless function calls?

+2  A: 

i found in this link :

http://phplens.com/lens/php-book/optimizing-debugging-php.php

A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.

Update: 11 July 2004: The above test was on PHP 4.0.4, about 3 years ago. I tested this again in PHP4.3.3 and calling a function now takes about 20 $localvar++ operations, and calling a method takes about 30 $localvar++ operations. This could be because $localvar++ runs faster now, or functions are slower.

Haim Evgi
+2  A: 

I would go with something more event-natured like this work around for PHP's missing event support. It looks similar to what you had in mind with pre-registering.

If you can't do that or prefer not to, a bunch of empty function calls will not hurt performance (though thousands of them? Maybe). I'd do some basic profiling to see where time is spent serving a page. If it is shown that empty functions take a significant amount of time then I would worry about it.

I cannot imagine a case where the page is so complex as to require that many triggers, though, and not have other more imposing bottlenecks. That is, if the page has so much going on, the added triggers become even less significant.

Michael Haren