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.
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?
– Augusto Vasques
@Augustovasques If you think it will be interesting to bring here, feel free :D Understanding motivation is always important
– Woss