A few days ago I was going through the a PHP RFC page when I found a vote for deprecation of a cast I had never used before, a cast to NULL. The keyword unset is used for this cast, for example, $b = (unset) $a, which results in $b being assigned with the value NULL, and nothing else. This should not be confused with the function unset, which really unsets the value.

I could not find any actual usage of this casting type on GitHub, except for some tests, and other useless stuff. A Google search also falls short of anything interesting, except for a question on StackOverflow, related with this type cast, asking for what is the point of it. Only the latest answer, by [Mihailoff], has a plausible use for this type casting. Take a look at this example:

$someBool = false;
$result = $someBool ? 'foo' : (unset) fallback();

This allows the variable $result to be either 'foo' or NULL, but in the latter, we still run a function to do something, and ignore its value. However this could be done in way which is easier to read:

$someBool = false;
if ($someBool)
    $result = 'foo';
else {
    fallback();
    $result = NULL;
}

Of course that I see the trade-off there, but either way, I don't think I have ever seen the use of this cast before. In C and C++ it is somewhat common to cast things to void*, but note that it is a pointer to something without a type. This is used for example in OpenGL, to pass arbitrary type buffers to the GPU.

In the same question mentioned above, there is an answer which contains a bug report related with this cast, because even the manual was not clear to how it worked. In this bug report, PHP maintainer [frozenfire] states that "That casting type exists purely for completeness, and you may as well just use NULL.". However, I decided to dig up when this feature was implemented, and turns out it was implemented 17 years ago, on 31 Dec 1999. It is a very old feature that persisted through time without any apparent usage.

If you liked this post or have any question, feel free to hit me up on twitter @AndreBaltazar.

Links: