What is the purpose of unset as cast in PHP?

Asked

Viewed 350 times

6

As of PHP 5, there is a way to cast in order to convert a certain value to NULL.

Example:

$teste = 'teste';

var_dump((unset)$teste); // NULL

$outroTeste = (unset) funcao();

var_dump($outroTeste); // NULL

I can understand that unset($var) destroys a variable, but the expression (unset) $var converts the variable to NULL.

But finally:

  • What is the purpose of this implementation - the unset as cast?

  • $var = NULL; would no longer be enough?

  • Is there any case where it would be important to use cast to unset (unset)?

  • From what little I understand and read in several blogs of PHP experts, most or almost all are unanimous in the use of unset for micro optimizations. I don’t have links now, but if you want to know more, search for "php micro Optimization unset" or something like that. I always use on all objects that will no longer be used during the script.

  • 1

    Related: http://answall.com/a/23737/250

2 answers

5


What is the purpose of this implementation?

Like explained by Pope Charlie, it uses type conversion syntax to generate a value null. In language it is possible to do casting for any primitive type of language, such as strings and numbers, including the type null. It’s strange, but less strange than choosing the syntax (unset) instead of (null).

$var = NULL; would no longer be enough?

Would, use a null literal is enough in most cases.

Is there any case where the use of (unset)?

"Important" would be too strong a term, but a guy in the English OS says he uses it to untangle a chain of parolees, applying on the return of a function, and as part of a conditional expression.

PHP 7.2

From PHP 7.2 the cast (unset) is in disuse, which indicates that it will probably be removed in a future version of PHP:

  • thanks! it seems that in the case of the last answer, it really becomes a useful expression (although I don’t want to use it like this)

  • 2

    Good update @Guilhermenascimento.

4

What is the purpose of this implementation?

I believe you’re referring to the use (unset) $var. This form is related to the typing of the variable, as well as (int) $var, or (string) $var... the use of (unset) $var will change the type of the variable to null.

Casting a variable to null using (unset) $var will not remove the variable or unset its value (DOC)


$var = NULL would no longer be enough?

There are times when you may need to destroy a variable and not simply have it null, and when using $var = NULL, $var will continue to exist. However, to change guy, yes, $var = NULL could be sufficient since it has the same effect as (unset) $var.

However, if you maintain a standard for typing, the use of (unset) $var may be more convenient than $var = NULL, because in the second case you are changing the type assigning a new value, rewriting the variable.

An example:

#1
$var = (int)'123'

#2
$var = 123

In both my cases $var is an int type. In the first I forced string typing to integer and in the second it assigns an integer value.


Is there any case where the use of (unset)?

About this there is much explanation in itself DOC function, but I don’t know if you refer to typing (unset) $var or the removal of the variable with unset( $var ).


I don’t know if that’s exactly what you wanted to know. If need be I try to make an update.

  • 2

    The staff passed the negative with a ferocity... Not even the DOC was forgiven :) Maybe another good soul can illuminate my failure.

  • I think your answer was well elaborated yes :). But I was still in doubt as to whether PHP has implemented this functionality, whether it is really usable or not (because, for not having found a utility, it seems that it is a useless feature)

  • This can be a personal matter. I found it interesting to change the typing without having to reset a value. Your question is when to use?

  • After so long, I decided to slap the details of the question :). And I didn’t give the -1

  • 1

    @Wallacemaxters and neither do I :) rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.