Bitten by PHP 4’s pass by value semantics

This post is more than 17 years old.

Posted at 23:05 on 07 February 2007

For those of you who know a bit about PHP and WordPress internals and are interested, here is a brief technical overview of the details behind the bug in the original version of Comment Timeout.

I was originally tweaking the comment status of the posts at the start of the loop. The way I was doing it was admittedly a hack, but it worked — in PHP 5 at any rate, which has pass-by-reference semantics. However, it fell over in PHP 4, where variables are all passed by value rather than by reference.

It actually turns out that there is a non-hack way of achieving this. This evening I discovered that WordPress provides a filter, the_posts, which gives you a much better way of doing it. It passes an array containing all the posts returned by a query to your filter; your plugin modifies the array as appropriate — in this case, setting the comment status on some of the posts to “closed” — and returns it. The latest version (1.3 alpha 2) now adopts this approach.

Unfortunately, a lot of the WordPress API documentation is somewhat incomplete and sketchy, especially on the more technical side, which is probably why I missed this the first time round. Perhaps as plugin authors we ought to chip in and spend a bit of time polishing it up as we go? It is a wiki after all, so we’ve only ourselves to blame if it’s not up to scratch.