Skip to content
PHP Logo

How can syntax errors like unexpected 'private' be avoided in PHP code?

To avoid syntax errors like unexpected 'private', ensure that access modifiers (public, protected, private) are used only within class definitions for class properties or methods. If 'private' is used outside of a class, it will result in a syntax error. To fix this issue, make sure that 'private' is used appropriately within class definitions.

php
class MyClass {
    private $property;

    public function __construct($value) {
        $this->property = $value;
    }

    public function getProperty() {
        return $this->property;
    }
}