Re: Re: Allowing is_* functions to accept multiple parameters
| From: | Patrick ALLAERT | Date: | Wed, 18 Sep 2013 14:48:49 +0000 |
| Subject: | Re: Re: Allowing is_* functions to accept multiple parameters | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-69198@lists.php.net to get a copy of this message | ||
2013/9/18 Sean Coates <sean@seancoates.com>:
>> i.e. is_null($a, $b, $c) would be the same as is_null($a) && is_null($b)
>> && is_null($c)
>
> Note that this would not be semantically equivalent in this form, even if
>
is_null() did accept multiple parameters, because of the
> short-circuiting with &&:
>
> <?php
>
> function are_null() {
> foreach (func_get_args() as $a) {
> if ($a !== null) {
> return false;
> }
> }
> return true;
> }
>
> function destroy_data() { echo "DESTROYING DATA\n"; }
>
> // old form: short-circuited; data is not destroyed
> if (is_null(null) && is_null(false) && is_null(destroy_data())) {
> echo "All null.\n";
> } else {
> echo "Not null.\n";
> }
>
> echo "----\n";
>
> // proposed form: no short-circuit; parameters are evaluated at call time and data is destroyed
> if (are_null(null, false, destroy_data())) {
> echo "Still null.\n";
> } else {
> echo "Still not null.\n";
> }
>
Not a good idea IMHO: it would complexify the execution a lot, think about:
$test = "are_null";
if ($test(null, false, destroy_data())) {
echo "Still null.\n";
} else {
echo "Still not null.\n";
}
Looking at the AST wouldn't be enough to tell what would or wouldn't
be short-circuited.
It looks fine to have is_* functions working on multiple values as
Leigh originally proposed. I also feel that it would mostly be
used/useful on variables.
People worrying about performance and/or execution of code with
short-circuiting can (and should) still rely on "&&".