Finding call-time pass by references in PHP.
While trying to move an older code base to a newer system and thus a newer version of PHP (5.3 -> 5.5), I knew that some of the code would need to be changed to avoid using some removed features. Specifically, I mean call-time pass by references. For those who don’t know, this is kind of a weird feature of earlier versions of PHP that allows one to call a function and pass any of the arguments by reference rather than the usual call by value if the caller prepends an argument variable with the “reference to” operator &
.
So, to illustrate, normally this code won’t have side effects because of call by value:
1
2
3
4
|
function do_me( $var ) {
$var = "I'm doing it: " . $var;
echo $var;
}
|
However there will be side effects if the caller chooses pass by reference:
1
2
|
$text = "happily";
do_me( &$text );
|
I thought a regex might be in order to find these guys and fix them:
1 |
ag -r -G '\.(php|module|inc|page|view)$' '^(?:(?!function).)*\([^&]*&[^&#][[:alpha:]$_].*\)'
|
but it was a naive idea, and this regex devolved (heh) to its current form before I realized I could just use the built-in linter to find the problem spots.
1 |
find . | grep -Fv '/\.svn' | grep -E '\.(php|module|inc|page|view)$' | xargs -n 1 php -l
|
HTH