Windows paths in PHP

Time for the PHP annoyance of the day: includes on Windows. PHP 4 has a nasty bug with the way it handles the require_once() and include_once() functions on Windows and I got bitten by it today.

If you don't know PHP, there are four functions to include code that's in other files: include(), require(), include_once(), and require_once(). The include function works the same way as #include in C: it just dumps the contents of the given file into the current one. The require() function does the same thing, but errors out the script if the file cannot be included for some reason.

Now, the *_once() varieties have a handy extra feature: if the given file has aleady been included, then they won't include it again. This is nice because it keeps you from needing to worry about errors caused by re-including the same fucntion or class library. The only problem with these functions is that they don't work correctly on Windows.

This problem came up while I was testing LnBlog on Windows. See, LnBlog stores each blog in a folder located outside the program directory, and the blog URL is actually the folder URL. It makes for a nice URL structure, but it means that the wrapper scripts that generate your pages have to be told where the LnBlog program files are. Well, at some point, while I was messing around with my test blog, I changed the path to the LnBlog directory.

Actually, that's not quite right. I changed the string that represents that path. The path that string referred to was actually correct. It's just that the path I gave was all lower-case, while the path on the filesystem was mixed-case.

It seems PHP 4 doesn't like it when you do that. Apparently it checks if a file has been included by storing the full path to each included file in a list and then doing a simple list search at each subsequent include. So one script include a require_once("lib/utils.php") and it would be included relative to the mixed-case current directory. That's fine. Then that script would include another files that did the same thing. Only this file apparently found the file relative to the include_path, which had the all lower-case path. Same path, but a different string representing it. Since PHP was apparently doing a simple string comparison to check if the file was included, it concluded that these were actually two different files and included the same one again. Bah!

Something of an exoteric bug, but still a pain. Although, to be fair, this is fixed in PHP 5. Not that it matters, because my target audience is still made up of people who don't necessarily have PHP 5.

To be honest, I'm getting a little sick of PHP. I was playing with Python again last week, and PHP is just painful by comparison. I'm starting to agree that it really is the "Visual Basic of the web." The only thing going against that impression is the fact that PHP treats Windows as a second-class citizen.

You can reply to this entry by leaving a comment below. You can send TrackBack pings to this URL. This entry accepts Pingbacks from other blogs. You can follow comments on this entry by subscribing to the RSS feed.

Add your comments #

A comment body is required. No HTML code allowed. URLs starting with http:// or ftp:// will be automatically converted to hyperlinks.