On Mon, Feb 11, 2019 at 8:39 AM Woortmann, Enno <enno.woortm...@web.de> wrote:
>
> Hi internals,
>
> as I reviewed a bunch of code for handling data from different sources
> (eg. json) in the last days I stumbled over code like this multiple times:
>
>
> if (!(is_numeric($input['example1']) && is_numeric($input['example2']))) {
>
>
> if (!is_numeric($input['example1'] || !is_numeric($input['example2'])) {
>
>
> and I had multiple problems with this.
>
> * it's kinda hard to read
>
> * multiple writings for the same logic
>
> * ends up in complex conditionals
>
>
> I searched for discussions regarding this topic and found it was
> mentioned in a 'side thread' of the RFC for changing empty() to a
> variadic a few years ago (https://externals.io/message/82549#82641) and
> I'd like to collect some feedback if it's wothy to revisit the topic to
> write the above example as:
>
>
> if (!is_numeric($input['example1'], $input['example2'])) {
>
>
> Except the is_callable() method all is_*() methods could be extended
> with a variadic behaviour which would check the given values from the
> left to the right and abort if a value doesn't match the condition (in
> the example if a given value is not numeric). So all in all there are
> some points to talk about: Revisit the discussion? Which functions are
> reasonable to be extended? If all functions are reasonable: what to do
> with is_callable()?
>
> regards,
> Enno
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php

My position is the same: pushing the variadic behavior into the
functions means that each function needs to pick `||` or `&&`
behavior, both of which are useful. I would rather see more
descriptive function names, such as `all_of` or `any_of`:

    if (!all_of('is_numeric', [$input['example1'],
$input['example2']])) {/*...*/}

These do not need to be part of PHP core, but perhaps they could be.

I recognize that there is one downside, which is that lazy evaluation
is lost, but generally don't see it to be an issue in these specific
cases.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to