Classes Constant Disambiguation With Parenthesis

<?php

class A { const B = 'A::B is a constant'.PHP_EOL; }

class C { const B = 'C::B is a constant'.PHP_EOL; }

const C = new A;

echo C::B;    // C is the class
echo (C)::B;  // C is the constant

// C is a class here
var_dump(new C instanceof C);
// (C) is the constant,
var_dump(new C instanceof (C));

// instanceof works with objects too
var_dump(new C instanceof (new C));

// but this is invalid syntax
//var_dump(new C instanceof new C);

?>

The :: (scope resolution) operator and the instanceof operator in PHP are strictly designed to work with class names, not with variables or constants holding object instances. Even when a constant exists with a name identical to a class, PHP will not automatically treat it as an object for these operators. The constant’s value, even if it holds an object, will not be used unless it is explicitly dereferenced. To dereference and force evaluation of the constant’s value as an object, you must enclose the constant in parentheses. This ensures PHP evaluates the constant and retrieves the object it contains before applying the operator.

See Also

PHP Features

Last updated: 14 July 2026