syntax error, unexpected token “public”

Description

The keyword public is expected in a PHP method body, but not in the body of a method or a function.

This may apply to the other visibilies, protected and private, and also to some other method options, such as abstract.

Example

<?php

class A {
    function foo() {
        public $x;
    }
}
?>

Solutions

  • Close the initial method body, before creating another property or method with a visibility.

  • Remove the public keyword in the body of the method.