📖 Scenario: You are working on a simple PHP program that deals with different types of vehicles. You want to check if a given object is an instance of a specific class to decide how to handle it.
🎯 Goal: Build a PHP script that creates vehicle objects and uses the instanceof operator to check their types.
📋 What You'll Learn
Create a class called Car
Create a class called Bike
Create an object of class Car named $myCar
Create an object of class Bike named $myBike
Use the instanceof operator to check if $myCar is an instance of Car
Use the instanceof operator to check if $myBike is an instance of Car
Print the results exactly as instructed
💡 Why This Matters
🌍 Real World
Checking object types is common when you work with different kinds of data or objects in a program, so you can handle each type correctly.
💼 Career
Understanding <code>instanceof</code> helps in writing clear and safe code, especially in object-oriented programming jobs like backend PHP development.
Progress0 / 4 steps
1
Create the Vehicle Classes
Create a class called Car and a class called Bike in PHP.
PHP
Hint
Use the class keyword to define classes in PHP.
2
Create Vehicle Objects
Create an object called $myCar from the Car class and an object called $myBike from the Bike class.
PHP
Hint
Use the new keyword to create objects from classes.
3
Check Object Types with instanceof
Use the instanceof operator to check if $myCar is an instance of Car and if $myBike is an instance of Car. Store the results in variables $isCar and $isBikeCar respectively.
PHP
Hint
Use the syntax: $variable = $object instanceof ClassName;
4
Print the Results
Print the values of $isCar and $isBikeCar using var_dump() to show if they are true or false.
PHP
Hint
Use var_dump() to print boolean values clearly.
Practice
(1/5)
1. What does the instanceof operator do in PHP?
easy
A. Creates a new instance of a class
B. Checks if an object is an instance of a specific class or its subclass
C. Deletes an object from memory
D. Converts an object to a string
Solution
Step 1: Understand the purpose of instanceof
The instanceof operator 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 B
Quick Check:
instanceof checks object type [OK]
Hint: Remember: instanceof checks object type, not creation or deletion [OK]
Common Mistakes:
Confusing instanceof with object creation
Thinking instanceof deletes objects
Assuming instanceof converts objects to strings
2. Which of the following is the correct syntax to check if $obj is an instance of class Car?
easy
A. if ($obj instance Car) {}
B. if ($obj is Car) {}
C. if (instanceof $obj Car) {}
D. if ($obj instanceof Car) {}
Solution
Step 1: Recall correct instanceof syntax
The correct syntax is: variable 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 D
Quick Check:
Correct instanceof syntax [OK]
Hint: Syntax is always: variable instanceof ClassName [OK]
Common Mistakes:
Using 'is' instead of 'instanceof'
Reversing order of variable and class
Adding extra keywords inside condition
3. What will be the output of the following PHP code?
$a = new DateTime();
if ($a instanceof DateTime) {
echo 'Yes';
} else {
echo 'No';
}
medium
A. Yes
B. No
C. Error
D. Nothing
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 DateTime is true because $a is exactly a DateTime object.
Final Answer:
Yes -> Option A
Quick Check:
Object is DateTime = Yes [OK]
Hint: If object matches class, instanceof returns true [OK]
Common Mistakes:
Assuming instanceof returns false for exact class
Expecting syntax error from instanceof
Ignoring else branch output
4. Identify the error in this PHP code snippet:
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof 'Animal') {
echo 'It is an animal';
}
medium
A. Missing semicolon after class definition
B. Incorrect object creation syntax
C. Using quotes around class name in instanceof
D. Using instanceof with a variable instead of object
Solution
Step 1: Check instanceof usage
The instanceof operator 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 C
Quick Check:
Class name must be unquoted in instanceof [OK]
Hint: Do not put quotes around class names in instanceof [OK]
Common Mistakes:
Putting class names in quotes inside instanceof
Confusing instanceof with string comparison
Missing semicolons elsewhere (not in this code)
5. Given the classes below, what will be the output?
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);
hard
A. Vehicle
B. Unknown
C. Car
D. Error
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 A
Quick Check:
Bike is a Vehicle, not Car = Vehicle [OK]
Hint: Check subclass before superclass in instanceof chains [OK]
Common Mistakes:
Expecting 'Car' for Bike object
Ignoring subclass relationship in instanceof
Assuming instanceof only matches exact class
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.