rfc:ifsetor

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:ifsetor [2008/06/21 20:30] – created lsmithrfc: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 <smith@pooteeweet.org>   * Author: Lukas Smith <smith@pooteeweet.org>
-  * Status: Under Discussion+  * Status: Declined
  
-This RFC proposes an operator that efficiently implements (isset($foo) ? $foo : $bar) as ifsetor($foo, $bar). 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://en.wikipedia.org/wiki/Null_%28SQL%29#COALESCE|COALESCE()]].+This RFC proposes an operator that efficiently implements (isset($foo) ? $foo : $bar) as ifsetor($foo, $bar). However this proposal has not been accepted, mainly due to the existance of the userland implementation #2. Of course once a true COALESCE() with any number of parameters could be supported, the situation would be different, since obviously using the "hack" of a pass by reference variable would require hardcoding the number of optional parameters.
  
 ===== 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, "admin"); $var = ifsetor($var, "admin");
 $var2 = ifsetor($var, "admin"); $var2 = ifsetor($var, "admin");
-</code> 
- 
-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 : "admin"); 
-</code> 
- 
-The proposal is that this could be written in a much more concise manner: 
- 
-<code php> 
-$var = ifsetor($var, $var2, "admin"); 
 </code> </code>
  
Line 53: Line 40:
 </code> </code>
  
-Furthermore ifsetor() would support expressions in all but the first parameter, that would only be evaluated if all of the previous parameters were either null or evaluated to null. In the following example the really_expensive_function() should not get called in most cases.+Furthermore ifsetor() would support expressions in the second parameter, that would only be evaluated if all of the previous parameter were either null or evaluated to null. In the following example the really_expensive_function() should not get called in most cases.
  
 <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, really_expensive_function());
 </code> </code>
  
Line 105: Line 93:
  
 This however is a deviation of the implementation of COALESCE() for example in SQLite, where at least [[http://www.sqlite.org/lang_corefunc.html|2 parameters are required]]. This however is a deviation of the implementation of COALESCE() for example in SQLite, where at least [[http://www.sqlite.org/lang_corefunc.html|2 parameters are required]].
- +==== userland #1 ====
-==== userland alternatives ====+
  
 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:
 </code> </code>
  
-A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http://marc.info/?l=php-internals&m=108940007627089&w=2|proposed on the list]]. Obviously it should be possible to implement a solution that could work with any number of parameters, all of which could be expressions. The use of pass by reference would prevent the notice from being thrown for the first parameter only. Obviously pass by reference variables cannot be optional or even dynamic. So this userland limitation would require hardcoding the number of parameters that would have no notices thrown for non existence. It also has some minor [[http://marc.info/?l=php-internals&m=111875921829669&w=2|implications on memory management]], since the below example would create a variable $foo with the value null in the current namespace (unless of course $x would be replaced with $foo in the below example).+==== userland #2 ==== 
 + 
 +A more flexible approach would make it possible to handle non arrays as well. The below is a simple implementation [[http://marc.info/?l=php-internals&m=108940007627089&w=2|proposed on the list]]. The use of pass by reference would prevent the notice from being thrown for the first parameter only. It also has some minor [[http://marc.info/?l=php-internals&m=111875921829669&w=2|implications on memory management]], since the below example would create a variable $foo with the value null in the current namespace (unless of course $x would be replaced with $foo in the below example).
  
 <code php> <code php>
Line 139: Line 128:
 ===== Proposal and Patch ===== ===== Proposal and Patch =====
  
 +Synopsis: "ifsetor" "(" value "," default ")"
  
 +  Returns the value if it exists or a given default value.
 +
 +Syntax:   "ifsetor" "(" variable [ "," expression ] ")"
 +
 +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://php.net/~helly/ze2-ifsetor-20040901.diff.txt
  
 ===== 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://en.wikipedia.org/wiki/Null_%28SQL%29#COALESCE|COALESCE()]]. 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 : "admin");
 +</code>
 +
 +The proposal is that this could be written in a much more concise manner:
 +
 +<code php>
 +$var = ifsetor($var, $var2, "admin");
 +</code>
  
 +However this is [[http://marc.info/?l=php-internals&m=108931281901389&w=2|currently not possible]] to be implemented without major slowdowns to the engine.
  
 ===== Changelog ===== ===== Changelog =====
Line 164: Line 176:
   * [[http://marc.info/?l=php-internals&m=111872386430805&w=2|Request to also add a custom callback to ifsetor()]] to increase the flexibility   * [[http://marc.info/?l=php-internals&m=111872386430805&w=2|Request to also add a custom callback to ifsetor()]] to increase the flexibility
   * [[http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-foo-isset-foo-foo-something-else|Decision at the PHP6 planning meeting to only implement ?: and not ifsetor]]   * [[http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-foo-isset-foo-foo-something-else|Decision at the PHP6 planning meeting to only implement ?: and not ifsetor]]
 +  * Suggestion to add [[http://marc.info/?l=php-internals&m=113210592810849&w=2|macro]] [[http://marc.info/?l=php-internals&m=116361059402342&w=2|support]] to make it easier to reuse complex userland code pieces to keep the code more readable when handling some of the above aspects in userland
 +  * [[http://marc.info/?l=php-internals&m=118946242013246&w=2|Request to add a native function to handle the limited case of a single dimensional array]] 
 +  * [[http://marc.info/?l=php-internals&m=108955534724882&w=2|Suggestion to fo with the ifsetor() as is]] and later try to find a way to implement a true coalesce() with the name "coalesce()"
rfc/ifsetor.1214080225.txt.gz · Last modified: 2017/09/22 13:28 (external edit)