Skip to content

Instantly share code, notes, and snippets.

@michitheonlyone
Created July 7, 2023 10:39
Show Gist options
  • Save michitheonlyone/81d93c40a7673469b0dd4206c985d772 to your computer and use it in GitHub Desktop.
Save michitheonlyone/81d93c40a7673469b0dd4206c985d772 to your computer and use it in GitHub Desktop.
The solution to find if a class exists in a array of classes
<?php declare(strict_types=1);
class A {}
class B {}
class C {}
$classes = [
new A(),
new B(),
];
var_dump( in_array(true, array_map(function ($c){ return $c instanceof A;}, $classes)) ); // returns true
var_dump( in_array(true, array_map(function ($c){ return $c instanceof C;}, $classes)) ); // returns false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment