The instanceof operator helps you check if an object belongs to a certain class or type. It is like asking, "Is this object a kind of this class?"
Instanceof operator in PHP
$object instanceof ClassName
The operator returns true if $object is an instance of ClassName or its subclass.
It returns false if $object is not an object or not related to ClassName.
$dog is an object of class Animal or a subclass.$dog instanceof Animal
$car is a Vehicle object.if ($car instanceof Vehicle) { echo "This is a vehicle."; }
true or false depending on whether $var is an instance of StringClass.$isString = $var instanceof StringClass;This program creates two objects: $dog from class Dog which extends Animal, and $car from class Car. It checks if each object is an instance of Animal. Since Dog is a subclass of Animal, the first check is true. The Car object is unrelated, so the second check is false.
<?php class Animal {} class Dog extends Animal {} class Car {} $dog = new Dog(); $car = new Car(); if ($dog instanceof Animal) { echo "Dog is an Animal\n"; } else { echo "Dog is NOT an Animal\n"; } if ($car instanceof Animal) { echo "Car is an Animal\n"; } else { echo "Car is NOT an Animal\n"; }
The instanceof operator works only with objects, not with simple types like strings or integers.
It also returns true if the object is an instance of a subclass of the given class.
instanceof checks if an object belongs to a class or its subclass.
It helps avoid errors by confirming object types before using them.
Returns true or false based on the check.
Practice
instanceof operator do in PHP?Solution
Step 1: Understand the purpose of
Theinstanceofinstanceofoperator is used to check if a variable is an object of a certain class or subclass.Step 2: Differentiate from other operations
It does not create, delete, or convert objects; it only checks their type.Final Answer:
Checks if an object is an instance of a specific class or its subclass -> Option BQuick Check:
instanceof checks object type [OK]
- Confusing instanceof with object creation
- Thinking instanceof deletes objects
- Assuming instanceof converts objects to strings
$obj is an instance of class Car?Solution
Step 1: Recall correct
The correct syntax is:instanceofsyntaxvariable instanceof ClassName, without extra keywords or rearrangement.Step 2: Check each option
if ($obj instanceof Car) {} matches the correct syntax exactly. Others have wrong keywords or order.Final Answer:
if ($obj instanceof Car) {} -> Option DQuick Check:
Correct instanceof syntax [OK]
- Using 'is' instead of 'instanceof'
- Reversing order of variable and class
- Adding extra keywords inside condition
$a = new DateTime();
if ($a instanceof DateTime) {
echo 'Yes';
} else {
echo 'No';
}Solution
Step 1: Understand object creation
$a is created as a new instance of the DateTime class.Step 2: Check instanceof condition
The condition$a instanceof DateTimeis true because $a is exactly a DateTime object.Final Answer:
Yes -> Option AQuick Check:
Object is DateTime = Yes [OK]
- Assuming instanceof returns false for exact class
- Expecting syntax error from instanceof
- Ignoring else branch output
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof 'Animal') {
echo 'It is an animal';
}Solution
Step 1: Check instanceof usage
Theinstanceofoperator expects a class name without quotes, not a string.Step 2: Verify other parts
Classes and object creation are correct; semicolons are present.Final Answer:
Using quotes around class name in instanceof -> Option CQuick Check:
Class name must be unquoted in instanceof [OK]
- Putting class names in quotes inside instanceof
- Confusing instanceof with string comparison
- Missing semicolons elsewhere (not in this code)
class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}
function checkVehicle($obj) {
if ($obj instanceof Car) {
return 'Car';
} elseif ($obj instanceof Vehicle) {
return 'Vehicle';
} else {
return 'Unknown';
}
}
$myBike = new Bike();
echo checkVehicle($myBike);Solution
Step 1: Analyze object type
$myBike is an instance of Bike, which extends Vehicle.Step 2: Follow function logic
First condition checks if $obj is Car - false. Second checks if $obj is Vehicle - true because Bike is subclass of Vehicle.Final Answer:
Vehicle -> Option AQuick Check:
Bike is a Vehicle, not Car = Vehicle [OK]
- Expecting 'Car' for Bike object
- Ignoring subclass relationship in instanceof
- Assuming instanceof only matches exact class
Ready to prove it?
Pass the test and this pattern moves unknown
