On Sun, Apr 10, 2011 at 10:04 AM, Rune Kaagaard <rumi...@gmail.com> wrote:
> Hey again > > Updated here as always https://gist.github.com/909711. I've tried to > write down the goal of this new feature. Which patterns do we want to > make easier to write? > > Do you agree with the goals I've set? > > +1 From me of course :) > > Also added a proposal that would make userland implementations possible. > Interesting ideas, everyone. However, I believe that the better approach would be to implement a new form of the ternary operator as I suggested above. Just to save on typing, I'll call the new form of the ternary operator a "Checked Ternary" in the remainder of the email (but please don't judge its merits on the basis of the name, the name can be anything you prefer.) I believe the checked ternary operator would prove to be a reasonable solution because: - I firmly believe it's good that PHP elicits a warning when one tries to access a non-existent element. We just need a way to work with arrays that affords more parsimony. - Changing the semantics of the existing ternary operator (as suggested by David) is not wise. I'm still trying to rally hosts to support PHP 5.3, so I'm very cautious about changes that impact the semantics of existing operators. - Developers are already familiar with the ternary operator, especially in the context of working with arrays and providing defaults. Providing an augmented form of the ternary operator will generalize well and be easy for developers to learn and remember. - In contrast to Sanford, I don't see the meaning of '?' and '??' as being disjunctive when compared to '=', '==', and '==='. - I don't see a need to mirror both empty() and isset(). *Checked Ternary Operator (expression1) ?? (expression2) : (expression3)* The checked ternary operator follows the same rules as the ternary operator. The difference between the checked version and the standard version is that expression1 expands to the code below automatically IF the first expression is a variable: (isset($exrpession1) && $expression1) ?? (expression2) : (expression3) *Examples* $arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3'); $var1 = 'val1'; $var2 = 'val2'; // standard ternary operator $arr['k3'] ? strtoupper($arr['k3']) : 'default'; // V3 $arr['k4'] ? strtoupper($arr['k4']) : 'default'; // elicits warning $arr['k3'] ? : 'default'; // v3 $arr['k4'] ? : 'default'; // elicits warning ($var1 == $var2) ? 'same' : 'different'; // different ($var1 == $var3) ? 'same' : 'different'; // elicits notice // checked ternary operator $arr['k3'] ?? strtoupper($arr['k3']) : 'default'; // V3 $arr['k4'] ?? strtoupper($arr['k4']) : 'default'; // default $arr['k3'] ?? : 'default'; // v3 $arr['k4'] ?? : 'default'; // default ($var1 == $var2) ?? 'same' : 'different'; // different ($var1 == $var3) ?? 'same' : 'different'; // different In short, you could use the checked ternary operator anywhere you'd normally use the standard ternary and the only difference in behavior would be the lack of warnings and/or notices for accessing unset array keys and/or variables. This would be a safe, simple, and practical way of dealing with the issue. Thanks for reading this far. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com