One of the weirdest things about Graal is how it handles
null. I have done serious amounts of testing and have finally figured out how it works.
null behaves exactly like an unset variable.
This means, if you go through your code and replace every instance of
null with
lolThisDoesntExist, it will work exactly the same.
HOWEVER, it is impossible to "set" null. This is the "special" thing about null.
Meaning, this code doesn't work:
Again, this is the only special thing about
null. Otherwise, it is the same as any other unset variable.
The reason
null can be confusing, is not because it is special, but because the weird behavior of unset variables.
To make this clear, I will use
lolunset instead of
null to show that this behavior is the same for any unset variable.
PHP Code:
echo(lolunset);
// "" : This is what happens when you echo any unset variable.
echo(@(lolunset));
// "" : First force it to a string, then echo it. Same as before.
echo(lolunset.type());
// "-1" : This is what gets echoed for the type of any unset variable.
temp.a = lolunset; // set a variable to another unset variable
echo(temp.a);
// "0" : NOTICE!!!! When you set a variable to an unset variable,
// it turns into 0!
echo(@(temp.a));
// "0" : Same.
echo(temp.a.type());
// "0" : 0 being the type of a number, this confirms that temp.a
// really does equal 0.
echo(lolunset == "");
// "1" : Comparing an unset variable to a string is true
echo(@(lolunset) == "");
// "1" : Forcing the unset variable to a string then comparing it to a string
// is true.
echo(lolunset == 0);
// "1" : Comparing an unset variable to 0 is true.
echo(@(lolunset) == 0);
// "1" : Forcing an unset variable to a string then comparing it to 0
// is still true.
echo(temp.a == "");
// "1" : Comparing a variable with 0 in it to a string is true.
echo(@(temp.a) == "");
// "0" : Forcing a variable with 0 in it to a string then comparing it to a
// string is FALSE. Weird as hell but it makes a difference if
// the coercion is done by @() or by == apparently.
//
// The same happens even if you temp.a = 0; directly, so it is unrelated to
// lolunset and null stuff. But worth noting.
echo(temp.a == 0);
// "1" : Comparing 0 to 0, is true, obviously.
echo(@(temp.a) == 0);
// "1" : Comparing 0 in string from to 0 is true as well.
You can replace
lolunset with
null in the above code, and it behaves exactly the same. The only difference between the two is that you can't overwrite
null with
true and screw up a whole program, so it's obviously a better idea to use
null than any other unset variable.
However, due to unset variables just being... weird (as seen above), I'd recommend always comparing to 0 or "" specifically, since it's a little less weird and you only need to think about the usual casting rules.
If you for some reason really need to check if a variable is unset, I'd say the best way is using
x.type() == -1.
Hope this saves someone else a headache and maybe sheds some light on the various weird dark corners of GS2.