is_a
is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool
Checks whether an object (or a class-name string, when allowed) is of a given type or a subtype; prefer the instanceof operator for objects.
C C23
#include <stdio.h>
#include <string.h>
/* C has no classes - the "is a" question becomes a manual type-tag check */
typedef struct {
int tag; /* 0 = shape, 1 = circle */
const char *name;
} Object;
static int is_a(const Object *obj, int class_tag) {
/* inheritance is flattened into a range check on the tag */
return obj->tag >= class_tag; /* circle tag 1 also satisfies shape tag 0 */
}
int main(void) {
Object circle = {1, "circle"};
printf("%d\n", is_a(&circle, 1)); /* same type */
printf("%d\n", is_a(&circle, 0)); /* supertype */
printf("%d\n", is_a(&(Object){0, "shape"}, 1)); /* parent is not a child */
return 0;
}
1 1 0
C++ C++26
#include <iostream>
class Shape {
public:
virtual ~Shape() = default; // polymorphic base enables dynamic_cast
};
class Circle : public Shape {};
class Square : public Shape {};
int main() {
Shape *s = new Circle();
std::cout << std::boolalpha;
// dynamic_cast to pointer returns nullptr on mismatch: the instanceof analogue
std::cout << (dynamic_cast<Circle*>(s) != nullptr) << "\n"; // same type
std::cout << (dynamic_cast<Shape*>(s) != nullptr) << "\n"; // supertype always ok
std::cout << (dynamic_cast<Square*>(s) != nullptr) << "\n"; // unrelated: no
delete s;
return 0;
}
true true false
C# v11.0.100
class Shape {}
class Circle : Shape {}
class Square : Shape {}
class MainClass {
static void Main() {
Shape s = new Circle();
// is: the instanceof analogue; true for the exact type and supertypes
System.Console.WriteLine(s is Circle); // same type
System.Console.WriteLine(s is Shape); // supertype
System.Console.WriteLine(s is Square); // unrelated: false
// as: the soft-cast variant - null instead of an exception on mismatch
Circle c = s as Circle;
System.Console.WriteLine(c != null);
}
}
True True False True
Go v1.27.4
package main
import "fmt"
// Go has no inheritance; embedding gives method promotion, not subtyping
type Shape struct{ name string }
type Circle struct{ Shape }
type Named interface{ Name() string }
func (s Shape) Name() string { return s.name }
func main() {
var obj any = Circle{Shape{"circle"}}
_, ok := obj.(Circle)
fmt.Println(ok) // same dynamic type
_, ok = obj.(Named)
fmt.Println(ok) // interface satisfaction: Shape's method is promoted
_, ok = obj.(Shape)
fmt.Println(ok) // embedding is not subtyping: a Circle is not a Shape value
}
true true false
Java v27.0.1
class Shape {}
class Circle extends Shape {}
class Square extends Shape {}
class Main {
public static void main(String[] args) {
Shape s = new Circle();
System.out.println(s instanceof Circle); // same type
System.out.println(s instanceof Shape); // supertype
System.out.println(s instanceof Square); // unrelated: false
// exact-type comparison knows no subtyping
System.out.println(s.getClass() == Circle.class);
System.out.println(s.getClass() == Shape.class);
}
}
true true false true false
JavaScript v26.13.1
class Shape {}
class Circle extends Shape {}
class Square extends Shape {}
const c = new Circle();
console.log(c instanceof Circle); // same type
console.log(c instanceof Shape); // walks the prototype chain: supertype
console.log(c instanceof Square); // unrelated: false
// typeof is far too coarse for classes - every object is just "object"
console.log(typeof c);
true true false object
PHP v8.6.0
// is_a: three-state type check - same type / supertype / unrelated
class Shape {}
class Circle extends Shape {}
$c = new Circle();
var_dump(is_a($c, Circle::class)); // same type
var_dump(is_a($c, Shape::class)); // supertype: parent class
var_dump(is_a($c, 'Countable')); // unrelated interface
$s = new Shape();
var_dump(is_a($s, Circle::class)); // parent is not a subtype of child
var_dump(is_a('Circle', Circle::class, true)); // string subject needs allow_string=true
var_dump($c instanceof Shape); // instanceof is the operator counterpart
// PHP 8.6: is_a('Circle', 'Shape') with allow_string=false is DEPRECATED
// (always false even when the class is loaded - such a call indicates a bug)
// is_a does NOT accept traits as the class argument
bool(true) bool(true) bool(false) bool(false) bool(true) bool(true)
Python v3.15.2
class Shape:
pass
class Circle(Shape):
pass
class Square(Shape):
pass
c = Circle()
print(isinstance(c, Circle)) # same type
print(isinstance(c, Shape)) # walks the MRO: supertype
print(isinstance(c, Square)) # unrelated: false
print(type(c) is Circle) # exact type, no subtyping
print(type(c) is Shape)
True True False True False
Rust v1.99.1
// Rust has no runtime class table; trait objects + Any downcast are the
// closest honest counterpart of instanceof
use std::any::Any;
struct Circle;
struct Square;
fn main() {
let obj: &dyn Any = &Circle;
// downcast_ref probes the erased concrete type behind the trait object
println!("{}", obj.downcast_ref::<Circle>().is_some()); // same type
println!("{}", obj.downcast_ref::<Square>().is_some()); // unrelated: no
}
true false
TypeScript v26.13.1
class Shape {}
class Circle extends Shape {}
class Square extends Shape {}
const c = new Circle();
console.log(c instanceof Circle); // same type
console.log(c instanceof Shape); // walks the prototype chain: supertype
console.log(c instanceof Square); // unrelated: false
// instanceof works at runtime; the type-level check is compile time only
console.log(c.constructor === Circle);
true true false true
Language differences
C: no OO runtime - is_a degrades into a hand-rolled type tag (example: a tag-range check emulating inheritance); the only real runtime type probing is symbol-level dlsym/dlsym.
C++: dynamic_cast<target*>(ptr) is the soft pointer check, returning nullptr on mismatch (example) - the base must be polymorphic (have a virtual function) for cross-type casts; typeid reveals the exact dynamic type but its name is implementation-defined and non-portable.
C#: is matches instanceof; as is the exception-free soft cast (example) - an is immediately followed by a cast should be merged into as + null check.
Go: the type assertion x.(T) checks "dynamic type == T" (first line of the example); interface satisfaction is a COMPILE-TIME concept - x.(I) checks the method set; embedding is not subtyping - a Circle embedding Shape is still not a Shape value (last line).
Java: instanceof is closest to PHP semantics (subtypes included); getClass() == X.class is an exact-type comparison that ignores subtyping (last two lines).
JavaScript: instanceof walks the prototype chain (example); it fails across realms (iframe/worker) whose prototypes differ; typeof reports every class instance as "object", too coarse.
PHP: for objects is_a($obj, X) equals $obj instanceof X, and the CONVENTION is to prefer instanceof; the class argument may be an interface; traits are not accepted. PHP 8.6 deprecation teaching point: a STRING subject with allow_string=false is deprecated (per the 8.6 migration list) - it is always false even when the class is loaded, signalling a bug; the correct pattern: class_exists($name, false) first, then call with allow_string=true. Under namespaces the class argument must be fully qualified - use does not apply inside strings.
Python: isinstance walks the MRO (example); type(c) is X is the exact comparison; duck-typing culture treats isinstance as a signal of ABC-system dependence.
Rust: compile-time binding, "does the type exist" is not a runtime question; the only runtime probe behind a trait object is Any::downcast_ref (requires 'static, example) - everyday Rust barely uses it.
TypeScript: instanceof behaves exactly like JS at runtime (example); type-level checks (x is Circle predicates, in-narrowing) are compile-time only.