Use an array filter :
if (count(array_filter($data, function ($entry) {
return !($entry instanceof CriterioDigital);
})) > 0) {
throw new \DomainException('an array of CriterioDigital must be provided');
}
Answer from ceadreak on Stack OverflowUse an array filter :
if (count(array_filter($data, function ($entry) {
return !($entry instanceof CriterioDigital);
})) > 0) {
throw new \DomainException('an array of CriterioDigital must be provided');
}
A colleague showed me an easy way to check, if an array contains instances of one class only:
class A {}
class B {}
class C extends A {}
$tests = [
1 => [new A(), new A()], // succeeds
2 => [new A(), new B()], // fails
3 => [new C(), new C()], // succeeds, because C extends A
];
foreach ($tests as $index => $collection) {
try {
\array_map(static function (A $object): void {}, $collection);
} catch (\TypeError $e) {
// Implement your error handling here.
}
}
Here's an example with output: https://3v4l.org/1IVg9
Hmm.. shouldn't is_array() check for instanceof ArrayAccess ?
php - How to check if variable is array?... or something array-like - Stack Overflow
Issue with instanceof checks with a `class-string<T>` param
Newest Questions - Stack Overflow
I find it kinda odd that is_array() does not check for instance of ArrayAccess. If you introduce collection like object in your code, you gotta recheck all of in_array() usage and change it to:
if (is_array($input) || $input instanceof \ArrayAccess) {And since the purpose of the ArrayAccess interface is 'to provide accessing objects as arrays' , shouldn't is_array() return true?
I know that my $input in that case is object, that's why is_array() returns false, but isn't that kinda 'logical' error?
The only reason I am using is_array() on an array/variable, is cause there is no other way to tell if I can run array related functions on it, aka if it fulfills the contract of ArrayAccess so I can access it as an array.
The information that actually it is an object or not, is secondary/irrelevant at this point to me.
Edit: forgot to add context, here it is:
if (is_array($input) || $input instanceof \ArrayAccess) {
array_walk_recursive($input, [$this,'sanitizeInputToken']);
return $input;
}Please note guys, I am not looking on how to solve this particular 'problem'. A wrapper method comes to mind or what not. I am just puzzled by PHP behaviour in that case.
Anyways thanks all for posting!
If you are using foreach inside a function and you are expecting an array or a Traversable object you can type hint that function with:
function myFunction(array $a)
function myFunction(Traversable)
If you are not using foreach inside a function or you are expecting both you can simply use this construct to check if you can iterate over the variable:
if (is_array($a) or ($a instanceof Traversable))
foreach can handle arrays and objects. You can check this with:
$can_foreach = is_array($var) || is_object($var);
if ($can_foreach) {
foreach ($var as ...
}
You don't need to specifically check for Traversable as others have hinted it in their answers, because all objects - like all arrays - are traversable in PHP.
More technically:
foreachworks with all kinds of traversables, i.e. with arrays, with plain objects (where the accessible properties are traversed) andTraversableobjects (or rather objects that define the internalget_iteratorhandler).
(source)
Simply said in common PHP programming, whenever a variable is
- an array
- an object
and is not
- NULL
- a resource
- a scalar
you can use foreach on it.