WTF! Superglobals in function declaration

With this post I start new category WTF – What The F**k. I will post here code samples which are raising only one question in my head every time i see them: “WTF?!”

The first WTF is using superglobals as argument names in function declaration in PHP. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
/**
 * Sample function
 *
 * Argument names equal to superglobal variables names which makes this
 * function behavior very unusual... and it`s look as well :)
 */
function my($_GET, $_POST)
{
    var_dump($_GET, $_POST);
}
 
// calling this method with arguments works as expected and actually
// overwrites superglobals inside function making them unavailable
// var_dump($_GET) inside function will dump array passed via arguments
my(array('fake' => 'var'), array());
 
// calling method without arguments leads to a few missing arguments
// warnings, but instead of NULL arguments superglobals get hit
// var_dump($_GET) will dump GET variables from supergloabs
my();

Leave a Reply