Reputation: 694
No, I'm not referring to the unset() language construct, but the (unset) type caster. From the PHP manual:
The casts allowed are:
- (int), (integer) - cast to integer
- (bool), (boolean) - cast to boolean
- (float), (double), (real) - cast to float
- (string) - cast to string
- (array) - cast to array
- (object) - cast to object
- (unset) - cast to NULL (PHP 5)
URL: http://php.net/manual/en/language.types.type-juggling.php
Does anyone have any idea about this (or even used it in an actual project)?
Upvotes: 15
Views: 1566
Reputation: 2192
The (unset) cast
has now been deprecated in PHP 7.2.x. So, don't use it anymore.
Ref: http://php.net/manual/en/migration72.deprecated.php#migration72.deprecated.unset-cast
Upvotes: 2
Reputation: 4914
According to php.net:
Example #2 Using (unset) casting
(unset) casting is often confused with the unset() function. (unset) casting serves only as a NULL-type cast, for completeness. It does not alter the variable it's casting.
So, the way I read it is "All types are castable, null
is a type so just for consistency we added a cast to null
"
Upvotes: 0
Reputation: 663
I use (unset)
casting to avoid creating ugly if
else
statements in situation if you need to validate and use many variables, but if one variable is considered incorrect and you do not want to check remaining.
For example, you have following code:
$variableone = "ffo";
$variabletwo = "obb";
$variablethree = "aar";
if(checkonefailed($variableone))
{
outputsomething();
}
else
{
dosomething($variableone)
if(checktwofailed($variabletwo))
{
outputsomething();
}
else
{
dosomething($variabletwo)
if(checkthreefailed($variablethree))
{
outputsomething();
}
else
{
dosomething($variablethree)
//And so own
}
}
}
You can rewrite it like this:
$variableone = "ffo";
$variabletwo = "obb";
$variablethree = "aar";
if(checkonefailed($variableone))
{
outputsomething();
}
//False or check
elseif((unset)(dosomething($variableone))||(checktwofailed($variabletwo)))
{
outputsomething();
}
elseif((unset)(dosomething($variabletwo))||(checkthreefailed($variablethree)))
{
outputsomething();
}
elseif((unset)(dosomething($variablethree))/*or next variable*/)
{
//And so own
}
Idea taken from Go programming code
if variableone := "ffo"; checkonefailed(variableone) {
outputsomething()
} else if dosomething(variableone); variabletwo := "obb"; checktwofailed(variabletwo) {
outputsomething()
} else if dosomething(variabletwo); variablethree := "aar"; checkthreefailed(variablethree) {
outputsomething()
} else dosomething(variablethree)
Upvotes: 2
Reputation: 23316
The only thing I can think of is some future use-case where a class can define how cast operators work, e.g as with per __toString()
. A class could potentially be marked non-nullable and hence casting to null would throw an exception. That would be fundamentally different from assigning NULL, although why on earth it's named unset
is a mystery.
Upvotes: 1
Reputation: 191729
I didn't even know this was a thing, but it seems like the purpose is just completeness for available php primitives (NULL being one of them). Note that this casts the data .. it does not do a write.
$x = 'foon';
$y = (unset)$x;
var_dump($x, $y) // 'foon', NULL
Note that x
is not null
in spite of the cast.
Near as I can tell, there's no reason to ever use (unset)<anything>
as opposed to just writing NULL
. Perhaps someone else can come up with a better answer, though.
Upvotes: 4
Reputation: 197682
The purpose is to cast to NULL, like you already wrote in your question:
(unset) - cast to NULL (PHP 5)
So let's say you have a variable $var
of which isset($var)
is TRUE
. You can then cast that variable to NULL
:
/**
* @param mixed $var anything
*/
function func($var)
{
if (isset($var))
{
$var = (unset) $var;
}
# the rest of the code expects $var to be NULL
...
}
Sure,$var = NULL
would do the same here, but w/o casting.
Upvotes: 0