rfc:ifsetor
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:ifsetor [2008/06/21 20:30] – created lsmith | rfc:ifsetor [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2008-06-21 | * Date: 2008-06-21 | ||
* Author: Lukas Smith < | * Author: Lukas Smith < | ||
- | * Status: | + | * Status: |
- | This RFC proposes an operator that efficiently implements (isset($foo) ? $foo : $bar) as ifsetor($foo, | + | This RFC proposes an operator that efficiently implements (isset($foo) ? $foo : $bar) as ifsetor($foo, |
===== Introduction ===== | ===== Introduction ===== | ||
Frequently PHP developers need to initialize some variable that is passed in via the outside to some value. This leads to repeated code, that is needlessly prone to typos as well as some performance overhead. | Frequently PHP developers need to initialize some variable that is passed in via the outside to some value. This leads to repeated code, that is needlessly prone to typos as well as some performance overhead. | ||
- | + | ==== Why do we need ifsetor()? ==== | |
- | ==== Why do we need RFCs? ==== | + | |
Frequently developers need to use the following code constructs: | Frequently developers need to use the following code constructs: | ||
Line 25: | Line 24: | ||
$var = ifsetor($var, | $var = ifsetor($var, | ||
$var2 = ifsetor($var, | $var2 = ifsetor($var, | ||
- | </ | ||
- | |||
- | In some situations code like the following will even be necessary to take into account multiple sources for a default | ||
- | |||
- | <code php> | ||
- | $var = isset($var) ? $var : (isset($var2) ? $var2 : " | ||
- | </ | ||
- | |||
- | The proposal is that this could be written in a much more concise manner: | ||
- | |||
- | <code php> | ||
- | $var = ifsetor($var, | ||
</ | </ | ||
Line 53: | Line 40: | ||
</ | </ | ||
- | Furthermore ifsetor() would support expressions in all but the first parameter, that would only be evaluated if all of the previous | + | Furthermore ifsetor() would support expressions in the second |
<code php> | <code php> | ||
function foo() { return (rand(0, 100) < 100 ? true : null); } | function foo() { return (rand(0, 100) < 100 ? true : null); } | ||
- | $var = ifsetor($var, foo(), really_expensive_function()); | + | $foo = foo(); |
+ | $var = ifsetor($foo, | ||
</ | </ | ||
Line 105: | Line 93: | ||
This however is a deviation of the implementation of COALESCE() for example in SQLite, where at least [[http:// | This however is a deviation of the implementation of COALESCE() for example in SQLite, where at least [[http:// | ||
- | + | ==== userland | |
- | ==== userland | + | |
Another misconception is that the functionality could be implemented in userland. This would prevent several key advantages of ifsetor(). | Another misconception is that the functionality could be implemented in userland. This would prevent several key advantages of ifsetor(). | ||
Line 121: | Line 108: | ||
</ | </ | ||
- | A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http:// | + | ==== userland #2 ==== |
+ | |||
+ | A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http:// | ||
<code php> | <code php> | ||
Line 139: | Line 128: | ||
===== Proposal and Patch ===== | ===== Proposal and Patch ===== | ||
+ | Synopsis: " | ||
+ | Returns the value if it exists or a given default value. | ||
+ | |||
+ | Syntax: | ||
+ | |||
+ | Semantic: | ||
+ | - The value in question must be a variable. | ||
+ | - The default value can be any expression. | ||
+ | - The default value can be omitted in which case NULL will be used. | ||
+ | |||
+ | http:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
+ | Actually ifsetor() is supposed to allow any number of parameters and it will return the first non null parameter (or null if there is none). This is analogues to the SQL function [[http:// | ||
+ | |||
+ | <code php> | ||
+ | $var = isset($var) ? $var : (isset($var2) ? $var2 : " | ||
+ | </ | ||
+ | |||
+ | The proposal is that this could be written in a much more concise manner: | ||
+ | |||
+ | <code php> | ||
+ | $var = ifsetor($var, | ||
+ | </ | ||
+ | However this is [[http:// | ||
===== Changelog ===== | ===== Changelog ===== | ||
Line 164: | Line 176: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * Suggestion to add [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// |
rfc/ifsetor.1214080225.txt.gz · Last modified: 2017/09/22 13:28 (external edit)