Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone ever used PHP's (unset) casting?

Tags:

I just noticed PHP has an type casting to (unset), and I'm wondering what it could possibly be used for. It doesn't even really unset the variable, it just casts it to NULL, which means that (unset)$anything should be exactly the same as simply writing NULL.

# Really unsetting the variable results in a notice when accessing it nadav@shesek:~$ php -r '$foo = 123; unset($foo); echo $foo;' PHP Notice:  Undefined variable: foo in Command line code on line 1 PHP Stack trace: PHP   1. {main}() Command line code:0  # (unset) just set it to NULL, and it doesn't result in a notice nadav@shesek:~$ php -r '$foo = 123; $foo=(unset)$foo; echo $foo;' 

Anyone ever used it for anything? I can't think of any possible usage for it...

Added:
Main idea of question is:
What is reason to use (unset)$smth instead of just NULL?

like image 786
shesek Avatar asked Aug 16 '11 16:08

shesek


People also ask

What's better at freeing memory with PHP unset () or $var Null?

null variable immediately frees the memory.

What does the unset () function do?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

What is the use of isset () and unset ()?

The isset () function is used to check whether a variable is set or not. If a variable is already unset with unset() function, it will no longer be set. The isset() function return false if testing variable contains a NULL value. More variable to be checked.

Which function is used to unset a variable?

The unset() function is a predefined variable handling function of PHP, which is used to unset a specified variable. In other words, "the unset() function destroys the variables".

Is it possible to unset a cast in PHP?

The (unset) cast is deprecated as of PHP 7.2.0, removed as of 8.0.0. Note: Because this is a language construct and not a function, it cannot be called using variable functions , or named arguments. It is possible to unset even object properties visible in current context. It is not possible to unset $this inside an object method.

What is PHP unset () and how to use it?

The following article provides an outline on PHP unset (). The primary operation of the method unset () is to destroy the variable specified as an input argument for it. In other words, it performs a reset operation on the selected variable. However, its behavior can vary depending on the type of variable that is being targeted to destroy.

What is the difference between (unset) cast and unset()?

The variable to be unset. Further variables. No value is returned. (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. The (unset) cast is deprecated as of PHP 7.2.0, removed as of 8.0.0.

Does casting a variable alter the variable it's casting?

It does not alter the variable it's casting. The (unset) cast is deprecated as of PHP 7.2.0, removed as of 8.0.0. Note: Because this is a language construct and not a function, it cannot be called using variable functions , or named arguments. It is possible to unset even object properties visible in current context.


2 Answers

As far as I can tell, there's really no point to using

$x = (unset)$y; 

over

$x = NULL; 

The (unset)$y always evaluates to null, and unlike calling unset($y), the cast doesn't affect $y at all.

The only difference is that using the cast will still generate an "undefined variable" notice if $y is not defined.

There's a PHP bug about a related issue. The bug is actually about a (in my mind) misleading passage elsewhere in the documentation which says:

Casting a variable to null will remove the variable and unset its value.

And that clearly isn't the case.

like image 121
John Flatness Avatar answered Dec 12 '22 05:12

John Flatness


I’d guess (knowing PHP and it’s notaribly... interesting choices for different things, I may be completely wrong) that it is so that the value does not need setting to a var. For exact reason to use it for a code, I can’t think of an example, but something like this:

$foo = bar((unset) baz());  

There you want or need to have null as argument for bar and still needs to call baz() too. Syntax of function has changed and someone did a duck tape fix, like what seems to be hot with PHP.

So I’d say: no reason to use it in well-thought architecture; might be used for solutions that are so obscure that I’d vote against them in first place.

like image 20
Smar Avatar answered Dec 12 '22 07:12

Smar