How will the null coalescence assignment operator work in PHP 7.4?

Asked

Viewed 217 times

9

In version 7.4 of PHP was implemented the null coalescence assignment operator that promises to unify the behaviors of assignment and null coalescence operators, the latter being present since version 7.0.

The idea is that the line

$var = $var ?? "default";

can be rewritten now as

$var ??= "default";

What will happen in this case?

In the RFC a slightly more complex example is cited:

// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';

// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';

What will happen in this case? What encouraged the creation of a new operator for this? Just to reduce written code? In what situations could such an operator actually be useful?

  • I can translate what motivated the programmer to create this operator. No github, but the text is short. More interesting are their considerations in the Stackexchage but it is too material to translate and put in response form. So I ask you, because it will take time and work, this translation is interesting and relevant to your question?

  • 1

    @Augustovasques If you think it will be interesting to bring here, feel free :D Understanding motivation is always important

1 answer

5


What will happen in this case?

It’s PHP, isn’t it? So you see, anything can happen :D But if all goes well and they made the right decisions (ok, they must have made it because they set this case as an example), if the content of $this->request->data['comments']['user_id'] is null or undefined so this variable (it completes) will have as value the text value. Note that it is an operator that causes side effect. As far as I understand it will create all the necessary values, and the example gives to understand this, but only testing to be sure.

What encouraged the creation of a new operator for this? Only to reduce written code?

I believe that only the simplification of syntax (shorten code) and syntax linearity since other operators of the type allows its composite form with the assignment, and of course this can reduce the chance of some error at the time of making the assignment (is a form of DRY), rare but can occur. I can’t see another reason.

How is PHP the pull request (RFC?!?!! ) and proposal does not explain anything conclusive or determines the motivation very strongly of something so important that will change in language, it seems that it was only a desire and not a studied need. Gives the impression that copied from another language without considering the peculiarities of PHP. Features work differently depending on others Features that exist in the language, has to be thought of all cases where will be used and say what is the result in all of them, is no different than making any software, only that the relationships are more complex than most software.

In what situations could such an operator actually be useful?

In the same places as the ?? was useful and kept the result in the evaluated variable itself, which is the overwhelming majority of cases of its use. It should not be used if you only use the result without storing in variable, such as a simple print, the return and any place that only requires an expression. Remembering that you should only keep in variable what you need to use more than once (there is the question of doing this for readability only instead of making a comment, but it should be used sparingly because it can even affect performance, and in this case it will keep in the same variable, so it will not leave more readable).

Certainty only when you can test and do what they didn’t do before implementing, can you even find cases that they didn’t think of.

  • If I do $var->foo->bar = $var->foo->bar ?? "ok" will work even when $var is not defined and so defines an object "recursively". Will the same behavior apply to that operator? If $var does not exist, defines it if the field foo does not exist, defines it, and finally if the field bar does not exist, defines it?

  • 3

    So that’s what I said, the proposal is crude and does not say anything about these things, it’s how everything is defined in PHP, it’s someone’s desire, does and sees what happens when people start using. You see clearly that the process doesn’t observe everything that can happen, it’s not people who understand programming languages that drive it. I think what happens is what you define, it creates everything, because it would be consistent, but it will know. Most current proposals make everything inconsistent. Certainty? Just waiting to test. It can be a surprise even for them.

  • 1

    In my opinion, saw this in C# and decided to do it too, only they did not realize that C# has no undefined variables. They do not realize that each Feature that you add to language creates a burden for you to carry for the rest of your life. Now they want to transform the language into Java, but bump into every legacy that was created to be a language of script, that is to do something quick and simple, not something Nterprise.

Browser other questions tagged

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