User Tools

Site Tools


conventions:php_null

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
php_null [2021/08/17 16:02] – created diogoconventions:php_null [2021/12/08 00:28] (current) – ↷ Page moved from php_null to conventions:php_null diogo
Line 1: Line 1:
 ====== The absence of value ====== ====== The absence of value ======
 +
 <markdown> <markdown>
-When programming in PHP it's common to have to represent the absence of value.+When programming in PHP it's common having to represent the absence of value.
 A variable that wasn't initialized yet or a function that could not produce a value. A variable that wasn't initialized yet or a function that could not produce a value.
 On the latter, one could be tempted to throw an exception in these scenarios, but not always that kind On the latter, one could be tempted to throw an exception in these scenarios, but not always that kind
Line 25: Line 26:
 ===== We've put to vote two options ===== ===== We've put to vote two options =====
  
-==== Option 1: Use === null ====+==== Option 1: Use === null (most of the time) ====
 <markdown> <markdown>
-On the discussion of whether to **use `=== null` vs [`is_null()`](https://www.php.net/manual/en/function.is-null.php)**, +Use one, or the other in function of context. This is better illustrated with two example situations:
-the literature online is diverse and divided. +
- +
-Some facts to consider: +
-* [null is both a data type, and a value](https://www.php.net/manual/en/language.types.null.php); +
-* As noted in PHP's documentation, the constant null forces a variable to be of type null; +
-* A variable with null value returns false in an [isset()](https://www.php.net/manual/en/function.isset.php) test, +
-  despite that, assigning a variable to NULL is _not_ the same as [unsetting](https://www.php.net/manual/en/function.unset.php) it. +
-  To actually test whether a variable is set or not [requires adopting different strategies per context](https://stackoverflow.com/a/18646568).  +
-* The [void return type](https://wiki.php.net/rfc/void_return_type) doesn't return NULL, but if used as +
-  an expression, it evaluates to null. +
-   +
-Therefore, in GNU social we would expect you to use one, or the other in function of context. +
-This is better illustrated with two example situations:+
  
 * If you're testing if a function returned null, then you're not testing a variable's data type, you're testing * If you're testing if a function returned null, then you're not testing a variable's data type, you're testing
Line 49: Line 37:
 would with a `is_int($var)`  or `is_countable($var)`. would with a `is_int($var)`  or `is_countable($var)`.
      
-About [nullable types](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.union.nullable), 
-we prefer that you _use_ the shorthand `?T` instead of the full form `T|null` as it suggests that you're considering the 
-possibility of not having the value of a certain variable. This is reinforced by the fact that NULL can not be a 
-standalone type in PHP.  
 </markdown> </markdown>
 ==== Option 2: Use is_null() ==== ==== Option 2: Use is_null() ====
Line 75: Line 59:
 A non-void function, by definition, is expected to return a value. A non-void function, by definition, is expected to return a value.
 If it couldn't and didn't run on an exceptional scenario, then you should test in a different style from that of regular If it couldn't and didn't run on an exceptional scenario, then you should test in a different style from that of regular
-strict comparison. Hence, as you're testing whether a variable is of type null, then you should use `is_null($var)`.+strict comparison. Hence, as you're testing whether a variable resulting of a union of types is of type null, then you should use `is_null($var)`. It's about verifying if a variable is in a state in which holds a value.
 Just as you normally would with an `is_int($var)`  or `is_countable($var)`. Just as you normally would with an `is_int($var)`  or `is_countable($var)`.
 </markdown> </markdown>
Line 82: Line 66:
  
 <markdown> <markdown>
-Voter|is_null|=== null +Voter    `is_null()` `=== null` 
------|-------|--------+---------|-------------|--------
 +XRevan86 |X            |- 
 +Diogo    |X            |- 
 +Hugo     |-            |X 
 +Eliseu   |X            |X
  
 </markdown> </markdown>
conventions/php_null.1629216147.txt.gz · Last modified: 2021/08/17 16:02 by diogo