rfc:ifsetor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:ifsetor [2008/06/21 20:36] – 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 41: | 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 93: | 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 108: | Line 107: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== userland #2 ==== | ||
A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http:// | A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http:// | ||
Line 124: | Line 125: | ||
The obvious problem is that all expressions would always be evaluated, even if their return value will not be used. This brings with it a significant performance overhead and can even lead to bugs if the expression creates side effects that should only occur if the expression was actually used to generate the value for the variable. | The obvious problem is that all expressions would always be evaluated, even if their return value will not be used. This brings with it a significant performance overhead and can even lead to bugs if the expression creates side effects that should only occur if the expression was actually used to generate the value for the variable. | ||
+ | |||
===== Proposal and Patch ===== | ===== Proposal and Patch ===== | ||
Line 137: | Line 139: | ||
- The default value can be omitted in which case NULL will be used. | - The default value can be omitted in which case NULL will be used. | ||
- | http://marcus-boerger.de/php/ext/ze2/ | + | http://php.net/~helly/ |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Line 176: | Line 178: | ||
* Suggestion to add [[http:// | * Suggestion to add [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// |
rfc/ifsetor.1214080615.txt.gz · Last modified: 2017/09/22 13:28 (external edit)