Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The instanceof operator is a binary type operator used to determine if a given variable is an instantiated object of a specific class, a descendant of that class, or an implementer of a specified interface. It evaluates the object’s inheritance hierarchy at runtime and returns a boolean value.
$result = $object instanceof ClassName;

Operand Specifications

  • Left Operand ($object): The variable being evaluated. If the evaluated variable is not an object (e.g., a string, integer, or null), instanceof safely evaluates to false without emitting a warning or type error.
  • Right Operand (ClassName): The target type identifier. This can be an unquoted class or interface name, an object instance, or a variable containing a string of the fully qualified class/interface name.

Resolution Rules

The operator evaluates to true under any of the following conditions:
  1. Exact Match: The left operand is an instance of the exact class specified by the right operand.
  2. Inheritance: The left operand is an instance of a class that extends the right operand’s class at any point in the inheritance tree.
  3. Polymorphism (Interfaces): The left operand is an instance of a class (or extends a class) that implements the interface specified by the right operand.

Syntax Mechanics and Evaluation

interface Runnable {}
class BaseProcess implements Runnable {}
class BackgroundProcess extends BaseProcess {}

$process = new BackgroundProcess();

// 1. Exact Match
$process instanceof BackgroundProcess; // true

// 2. Inheritance Match
$process instanceof BaseProcess;       // true

// 3. Interface Match
$process instanceof Runnable;          // true

// 4. Non-Object Evaluation
$nonObject = "BackgroundProcess";
$nonObject instanceof BaseProcess;     // false

Dynamic Evaluation and Right Operand Variations

The right operand accepts dynamic values, but strict syntax rules apply regarding string literals.
$targetClass = 'BackgroundProcess';
$otherProcess = new BaseProcess();

// Using a string variable containing the class name
$process instanceof $targetClass;  // true

// Using another object instance (checks if $process is an instance of $otherProcess's class)
$process instanceof $otherProcess; // true

// SYNTAX ERROR: Direct string literals are not permitted
// $process instanceof 'BackgroundProcess'; 

// PHP 8.0+: Arbitrary expressions are allowed if wrapped in parentheses
$process instanceof ('Background' . 'Process'); // true

Operator Precedence and Negation

The instanceof operator has higher precedence than the logical NOT operator (!). When negating an instanceof expression, the type check evaluates first, followed by the negation. While wrapping the expression in parentheses is a common practice for human readability, the unparenthesized version is perfectly valid and functionally correct.
// Valid negation: evaluates natively as !($process instanceof Runnable)
$isNotRunnable = !$process instanceof Runnable;

// Equivalent expression using parentheses for explicit readability
$isNotExplicit = !($process instanceof Runnable);
Master PHP with Deep Grasping Methodology!Learn More