PHP upgrade woes
Sunday, 27 November 2005
There’s been lots of noise about PHP 5.0.5 breaking application code which had been happily running for quite some time under previous versions.
Given the following two functions:
function foo(&$stuff) {
echo $stuff;
}
function bar() {
return "hello world";
}
This bit of code will not output the string “hello world”, as might be expected:
foo(bar());
Instead you’ll cop this:
"Fatal error: only variables can be passed by reference."
In PHP 5.0.5, you can’t pass the results of a function call to a function that takes an argument by reference. The correct way to do this is:
$bar = bar();
foo($bar);
The alternative quick and dirty hack will also work:
foo($bar = bar());
Is this a bug? No, it’s not according to the PHP developers. It appears that what they’re saying is that passing the results of a function call to a function that takes a reference was always sloppy programming anyway, and their official response to this is: “won’t fix”, so there.
For most of us PHP developers, this is a bit like a poke in the eye with a toothpick. To improve security, the hosting company for most of the web applications we support have had to upgrade to PHP 5.0.5 recently, but that’s left us with having to review all the code we had written.
Apart from our own custom functions which use arguments by reference, we found that the bits that were suddenly exploding were mainly attributed to array functions, including:
array_pop() arsort() ksort() rsort()
array_push() asort() natcasesort() shuffle()
array_shift() current() natsort() sort()
array_splice() each() next() uasort()
array_unshift() end() pos() uksort()
array_walk() key() prev() usort()
array_walk_recursive() krsort() reset()
Have fun burning the midnight oil. We did.
|