Falling off the ob_start stack
Recursive output buffering is a great tool in web-oriented programming, but it can go _really_ wrong.
Caleb and I spent a large chunk of yesterday finding out a bunch of bugs that had been caused by incorrect use of output buffering.
PHP lets you call some commands called ob_start and ob_get_clean, which let you buffer content and save it into a variable. This is great as it allows you to do use the templating abilities to PHP to place output in variables. This lets you do things like:
<?php
function my_output($name) {
ob_start();
?>
Hi <?= $name ?>, how's it going today?
<?
return ob_get_clean();
}
What had happened in our case however, was that functions of the form:
function my_output($blah) {
ob_start();
really_long_function_call();
return ob_get_clean();
}
Had been turned into:
function my_output($blah) {
ob_start();
if($cache = cached('really_long_function_call') {
return $cache;
really_long_function_call();
return ob_get_clean();
}
The ob_start() stack was now out of whack. Whenever the top level layout code then tried to close an ob_start(), it would actually close the level that had been started in my_output(), which would cause page elements to show up out of order.
To make things more interesting, when PHP reaches the end of a program, it closes all open buffers and flushes their output out to its output. This really through us off as we spent the first hour of debugging into looking why the buffered output was being flushed when it shouldn't, when this actually wasn't happening.
This is one of those standard cases that when you suspect the bug is in the system libraries instead of your code, you're probably doing something wrong.
TechWorld Jobs (beta)
Whitepapers
TechWorld Blogs
-

TalkingTech
The view from the top of IT with TechWorld Editor Rodney Gedda
-

Entrenched
Cooking up better code, IDG's developers reveal some of their secrets
-

Broadband Voice
Darren Pauli digs in from the front line of Australia's broadband battleground
Recent blog posts
- Telstra kicked out of NBN process
- Linux on the iPhone won’t change the world - yet
- A Novell approach to business
- An open storage stack? I like the sound of that
- The mobile clone wars: fighting for a better phone experience
- Stopping the "Clean Feed"
- Identifying web platforms
- Clean Feed ‘not technically possible’
- No Clean Feed - well duh!
- Conroy's content cops still on the cards








Recent comments
18 hours 50 min ago
18 hours 55 min ago
19 hours 2 min ago
19 hours 3 min ago
19 hours 3 min ago
19 hours 4 min ago
19 hours 9 min ago
19 hours 11 min ago
19 hours 12 min ago
19 hours 14 min ago
19 hours 15 min ago
19 hours 17 min ago
19 hours 19 min ago
19 hours 21 min ago
19 hours 23 min ago
19 hours 24 min ago
19 hours 26 min ago
19 hours 27 min ago
19 hours 27 min ago
19 hours 28 min ago