Specify Null Coalesce Operator isset Relation#5527
Specify Null Coalesce Operator isset Relation#5527Krause-a wants to merge 3 commits intophp:masterfrom
Conversation
Make the connection to `isset` explicit and distinct from the null check to match PHP behavior. Remove redundant warning suppression information.
| <replaceable>expr2</replaceable> if <replaceable>expr1</replaceable> is not set, | ||
| as determined by <function>isset</function>, or is &null;. | ||
| Otherwise, it evaluates to <replaceable>expr1</replaceable>. |
There was a problem hiding this comment.
It should be noted that isset() returns false is the value is defined but set to null.
While I think the wording here could be improved to make the behavior clearer, I think the suggested change here potentially causes confusion about the behavior of isset.
It might be better to omit mention of isset here entirely and just say:
if expr1 is not defined or is &null;.```
There was a problem hiding this comment.
Thank you for the feedback!
The driving factor for the inclusion of isset() is that both the isset check and the null check appear to be performed separately. I understand that this is redundant for array checks and variable checks. However, the seemingly redundant null check matters for object properties when using the __isset magic method.
class MyClass { # Minimal Example Class
public function __isset($name) {
return true;
}
public function __get($name) {
return null;
}
}
$my_obj = new MyClass;
# Ex.1
var_dump($my_obj->fake ?? 'rhs'); # rhs
# Ex.2
var_dump(isset($my_obj->fake) ? $my_obj->fake : 'rhs'); # null
# Ex.3
var_dump(isset($my_obj->fake) && $my_obj->fake !== null ? $my_obj->fake : 'rhs'); # rhsYou can try the different combinations with __isset returning true and false and __get returning null or 123.
if expr1 is not defined or is &null;
My worry is that this will be confused with only an isset check, since, in normal circumstances, isset will do the null check, making the second null check redundant. The documentation on isset starts with "Determine if a variable is declared and is different than null." The isset documentation sounds very similar. It could be believed that it is, in fact, exactly the same. The PHP 7.0 new features announcement includes the "... if it exists and is not null..." wording and references ?? as syntax sugar for an isset ternary. Do you have an alternative wording that would still have the isset and null check as distinct steps in the ?? operator?
There was a problem hiding this comment.
So it turns out this is a known oddity of isset() and the magic methods.
My personal feeling is that if this behavior is documented, it belongs under either (or both) the isset() or magic methods documentation.
The only change I would suggest to the null coalesce documentation, if any, is to say:
if expr1 is not set or is null
Related:
There was a problem hiding this comment.
Oh, wow! Thank you for the links! In light of this information, I agree with a less explicit reference to isset. Let me know what you think :)
Use simpara as per guidelines
Reduce `isset` tie in for possible future changes
This PR addresses issue #5526
Make the
issetrelationship with??explicit to reduce potential confusion.