syntax error, unexpected token “::”

Description

In this situation, the keyword or is reserved by PHP and cannot be used as a class name, a function name, or a global constant.

By extension, the or keyword cannot be used in static object notations, such as calling a constant, or a static method or property.

This error may arise with other keywords, such as switch, and, foreach, etc, for similar reasons. It also may happen when a new keyword is reserved by PHP, between two versions.

The error also appears when the left operand of the :: is missing, or not applicable. Here, there is an illustration with echo.

Example

<?php

// :: on an operator
1 or::A;

echo ::class;

// :: on a number
1::class;

?>

Solutions

  • Rename the class with a PHP non-keyword.