syntax error, unexpected identifier “%s”, expecting variable or “$”

Description

new works with objects, static properties and normal properties. It does not work with class constants, even if the constant is an object.

Example

<?php

class A  {
    const B = C;
}

const C = new A;

var_dump(new A::B);

?>

Literal Examples

  • syntax error, unexpected identifier “B”, expecting variable or “$”

Solutions

  • Store the constant in a variable, and new the variable.

  • Get the class name with get_class or ::class and then, create the new object.