syntax error, unexpected variable “$this”, expecting “;” or “{”

Description

An abstract method’s signature must be terminated with a semicolon, since it has no body, and a concrete method’s signature must be followed by a { opening the body. Here, the variable $this was found right after the closing parenthesis of the argument list, where PHP expected one of those two terminators.

$this cannot appear here in any case: it is a read-only pseudo-variable, automatically bound to the current object inside a non-static method, and it can never be declared, assigned or used as a standalone statement outside of an expression.

Example

<?php

abstract class X {
    abstract function foo() $this;
}

?>

Solutions

  • Remove $this and terminate the abstract method with a semicolon.

  • If a method body was intended, replace $this with { and the method’s code.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.