Re: Implicit isset/isempty check on short-ternary operator

From: Date: Sun, 10 Apr 2011 14:04:06 +0000
Subject: Re: Implicit isset/isempty check on short-ternary operator
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Groups: php.internals 
Request: Send a blank email to internals+get-51854@lists.php.net to get a copy of this message
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. ## Goal ## So what should be solved before this new feature can be considered a succes? The solution should offer DRY solutions to the following patterns:    // Set one variable from another or a default value.    // 1a.    $a = isset($b) ? $b : 'Default value';    // 1b.    $a = !empty($b) ? $b : 'Default value';    // Set one variable from itself or a default value.    // 2a.    $a = isset($a) ? $a : 'Default value';    // 2b.    $a = !empty($a) ? $a : 'Default value';    // Find the first set value from a range of variables and statements..    // 3a.    $tmp = get_value();    if (isset($tmp)) $a = $tmp;    $tmp = $myarr['mykey'];    if (!isset($a) && isset($tmp)) $a = $tmp;    if (!isset($a)) $a = 'Default value';    // 3b.    $tmp = get_value();    if (!empty($tmp)) $a = $tmp;    $tmp = $myarr['mykey'];    if (!isset($a) && !empty($tmp)) $a = $tmp;    if (!isset($a)) $a = 'Default value'; ## Proposal 2 : Making a userland implementation possible ## The one thing preventing us PHP end users for implementing our own solution to this problem is - as discussed many times before - that it is not possible to check if a passed argument can be referenced and then reference it. This proposal adds an AS_REFERENCE bitmask that can be passed as the second argument to func_get_arg which then in turn throws an ArgNotReferencableException if the argument is not referenceable. Below is an example of how firstset (ifsetor) could be implemented.    function firstset() {        $num_args = func_num_args();        for ($i = 0; $i < $num_args; ++$i) {            try {                $arg = func_get_arg($i, AS_REFERENCE);                if (!isset($arg) {                    $arg = null;                }            } catch (ArgNotReferencableException $e) {                $arg = func_get_arg($i);            }            if (isset($arg) {                return $arg;            }        }        return null;    }

« previous php.internals (#51854) next »