Ok, so let me try to do a little summary.
If and When
-----------
1) There seems to be a general consensus that this feature should be implemented in SOME way.
2) It was too late for 5.0.0 so it will be targeted for 5.1
What ---- The requirements seem be: a) return the first variable/expression that isset() b) no error shouldbe thrown for unset variables c) short circuit evaluation
How
---
1) ?: has been rejected for the following reasons.
- Possible confusion with the ternary operator
- Asymmetry with the ternary operator - since it tests isset()
- It is non-obvious in its functionality in and of itself
- It would be difficult to find info on it in the Docs
- There is no WIDESPREAD use in other languages (though it is used in GCC) and PHP has been very strict about not introducing new operators that are not already widely used (only 2 in 10 years)
2) Jason Garber suggested the following syntax: $a = $b setor $c;
While I like the fact that it is an operator instead of function, and
therefore eases the process of chaining multiple operations together, I see a problem. Most PHP operators use non-alphanumeric characters, therefore making it easier to quickly see the difference between variable names and operators. I believe the primary exceptions are "or", "and", and "xor". These were most likely added because of a similarity with perl and can kinda get away with it since they are so short. "setor" just doesn't look like an operator to me.
3) So this leaves a function call which begs two questions: What do we call it, and what is the function syntax.
(a) What do we call it
Various suggestions have been rejected because of similarity to existing keywords. These include: ifset(), ifelse(), ifexists() and probably a few more.
We can't extend isset() perform this functionality because isset() already takes multiple parameters.
nvl(), ifnull() and coalesce() have been proposed because of similar existing functions in Oracle, MySQL and Postgres. However Marcus has pointed out that these function test for null while we are testing for existence. I also think that since these functions are 1) not WIDELY used and 2) not terribly intuitively named (with the possible exception of coalesce) we should probably not use them unless we are unable to come up with something else.
This currently leaves us with a hand full of names, including: ifsetor(), firstset(), getval()/getvalue()/get_value() (Is there a rule about underscores in new function names?).
(b) What is the function syntax?
Marcus has already looked into the issue and already created a patch, the existing patch only takes one variable and an optional default
value (any expression).
"ifsetor" "(" <variable> [ "," <expression> ] ")"
According to Marcus:
"Changing it to accept a list of *variables* could probably be possible but would require heavy restructuring of the compiler. The same is true for chaining it to accept a variable plus a list of expressions is even more complicated."
Christian seem to be intent on trying to make it work, preferably with the following format:
"coalesce" "(" <expr> ( "," <expr> )* ")"
Andi thinks we should "make a decision what the best way to go is and then we can discuss implementation if/what is possible". They will both probably not have time to look at this until after 5.0 is out.
My current preference is
"getval" "(" <expr> ( "," <expr> )* ")"
Marc
Marc Richards wrote:
On 4/15/2004 Jason Garber asked about a new language construct to simplify testing if a variable isset() and assinging a default value for those that aren't. The thread title was "Construct Request".
I rember reading it while the discussion went on, I just went back and browsed through it again. Everyone seemed to agree the it was generally a good idea and there was some minimal amount of consensus on the ?: syntax, but I can't tell if this was ever implemented.
Was it? If not did I miss the reason why?
Marc
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php