If you want to retain the value of a variable between one call to a function and the next, you can declare it as static. And as part of the initial declaration, you can assign a starting value to it. This allows you to test whether you're calling a function for the first time, or for a subsequent time.
function getavail($stamp,$forevent = 0) {
static $haverooms = 0;
static $blocked, $held;
if (! $haverooms) {
$haverooms = 1;
$blocked = array();
$held = array();
etc
OK - so that's the
feature - so what's the
benefit?
Using a static in this way allows you to include code to initialise variables AND make use of them in the same logical block - making maintainance of code much easier than in would be if you were passing parameters around, or relying on
globals.
A block of code that runs only the first time a function is called is also useful if the function is required to do some heavy work that doesn't change within a running of your program; there's no point in re-calculating a value that you had already calculated, but lost when you exited from a function, is there? The code snippet above is a good example of this - over the New Year, I've been updating our online booking system to include hotel rooms as well as courses, and to dynamically refer back to the databases as it offers available rooms and courses to web site visitors. My
getavail function returns the number of (rooms) available on a certain date, passed in by the time stamp variable. The workings are quite intricate at times, and when we produce a calendar of availability we have to keep calling it for day after day .... so we
seed the $blocked and $held arrays during the first call - pre-loading tables of events, courses, prior bookings and closure periods which aren't going to change within the few milliseconds the program takes to run.
Are statics always useful? No - they are fiercely anti-OO. In other words, I would have a major problem if I wanted to extend my events tables, etc, to cover two different hotel objects. Ah - but I don't see us opening a second hotel in the near future.
(written 2007-01-04 08:15:20)
Associated topics are indexed under
H105 - PHP - Functions [2682] Adding extensions to PHP Open Source applications - callbacks - (2010-03-17)
[2630] Static variables and ampersands in PHP - (2010-02-10)
[2488] A variable number of arguments in a PHP function - (2009-11-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1380] Static variables in PHP - (2007-10-05)
[1357] Clean my plate, but keep my wine bottle. (PHP; Static) - (2007-09-18)
[1267] is there a lookup function in php? - (2007-07-15)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[866] A lazy programmer is a good programmer - (2006-09-15)
[775] Do not duplicate your code - (2006-06-23)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[409] Functions and commands with dangerous names - (2005-08-11)
[340] Code and code maintainance efficiency - (2005-06-08)
[339] Passing information into and out of PHP functions - (2005-06-07)
[308] Call by name v call by value - (2005-05-11)
[223] There is a function in PHP to do that - (2005-02-21)
[96] Variable Scope - (2004-10-22)
Some other Articles
Tomorrow's keywords - London, Training, Course, PHP, Ruby.Web site - a refresh to improve navigationFinding public writeable things on your linux file systemNo courses. No hotel guests. Rushed off our feet!PHP - static declarationParallel processing in PHPspan and div tags - a css comparisonOpen Source Courses and Business Hotel - products and prices for 2007css - handling white space and preModernising from tables to cascading style sheets