Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
PHP - static declaration
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 fiercly 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

Back to
Parallel processing in PHP
Previous and next
or
Horse's mouth home
Forward to
No courses. No hotel guests. Rushed off our feet!

Some other Articles
Tomorrow's keywords - London, Training, Course, PHP, Ruby.
Web site - a refresh to improve navigation
Finding public writeable things on your linux file system
No courses. No hotel guests. Rushed off our feet!
PHP - static declaration
Parallel processing in PHP
span and div tags - a css comparison
Open Source Courses and Business Hotel - products and prices for 2007
css - handling white space and pre
Modernising from tables to cascading style sheets
1637 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho