Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | Bruce Weirdan | Date: | Wed, 23 Dec 2020 22:39:28 +0000 |
| Subject: | Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112615@lists.php.net to get a copy of this message | ||
On Wed, Dec 23, 2020 at 7:44 PM Ben Ramsey <ben@benramsey.com> wrote:
> I actually like the idea of flags added to
is_array() for this.
>
> Something like:
>
> is_array($value, ZERO_INDEXED | ASSOCIATIVE | INTEGER_INDEXED)
>
For static analysis, though, unary predicate (e.g. is_list()) is much
easier to work with, secondary only to actual type. Flags may lead to
indirection, such as (contrived example)
```php
class C {
const FLAGS = ZERO_INDEXED | ASSOCIATIVE;
private function getFlags(): int {
return static::FLAGS;
}
public function foo(array $array): void {
if (is_array($array, $this->getFlags())) { ... }
}
```
--
Best regards,
Bruce Weirdan mailto:
weirdan@gmail.com