rfc:null_coercion_consistency
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:null_coercion_consistency [2022/05/05 11:51] – Add note about html templating engines craigfrancis | rfc:null_coercion_consistency [2022/05/09 10:53] – Update Backward Incompatible Changes (thanks Mel) craigfrancis | ||
---|---|---|---|
Line 45: | Line 45: | ||
- [[https:// | - [[https:// | ||
- | For example: | + | ==== Current State ==== |
<code php> | <code php> | ||
Line 116: | Line 116: | ||
==== Examples ==== | ==== Examples ==== | ||
- | |||
- | A simple search page: | ||
- | |||
- | <code php> | ||
- | $name = $request-> | ||
- | |||
- | if (trim($name) === '' | ||
- | $where_sql[] = 'name LIKE ?'; | ||
- | $where_val[] = $name; | ||
- | } | ||
- | |||
- | echo ' | ||
- | <form action=" | ||
- | < | ||
- | Search | ||
- | <input type=" | ||
- | </ | ||
- | <input type=" | ||
- | </ | ||
- | |||
- | if ($name !== NULL) { | ||
- | $register_url = '/ | ||
- | echo ' | ||
- | < | ||
- | } | ||
- | </ | ||
- | |||
- | Regarding the source of //$name// (line 1); while frameworks could change their default to an Empty String, or an automated tool could cast the variable to a string, doing so would break the "Add Account" | ||
- | |||
- | HTML Templating engines like [[https:// | ||
- | |||
- | I'd argue a very strict level of type checking (that prevents all forms of coercion) is best done by Static Analysis, which can check if a variable can be nullable, and it can decide if this is a problem, in the same way that a string (e.g. ' | ||
Common sources of NULL: | Common sources of NULL: | ||
Line 201: | Line 169: | ||
mail(' | mail(' | ||
</ | </ | ||
+ | |||
+ | HTML Templating engines like [[https:// | ||
+ | |||
+ | I'd argue a very strict level of type checking (that prevents all forms of coercion) is best done by Static Analysis, which can check if a variable can be nullable, and it can decide if this is a problem, in the same way that a string (e.g. ' | ||
There are approximately [[https:// | There are approximately [[https:// | ||
Line 343: | Line 315: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None | + | While the intention of this RFC is to avoid a BC break; for user defined functions to be updated to also coerce NULL (instead of throwing a Type Error), it's possible some code may rely on that behaviour, for example: |
+ | |||
+ | <code php> | ||
+ | function my_function(string $my_string) { | ||
+ | var_dump($my_string); | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | my_function(' | ||
+ | my_function(1); | ||
+ | my_function(1.2); | ||
+ | my_function(true); | ||
+ | my_function(false); | ||
+ | my_function(NULL); | ||
+ | } catch (TypeError $e) { | ||
+ | // Do something important? | ||
+ | } | ||
+ | </ | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/null_coercion_consistency.txt · Last modified: 2023/10/18 11:57 by craigfrancis