Cannot access %s property %s::$%s

Description

Properties have visibility, which restrict access to the defining class (private), or the class and its children (protected).

This applies to static and normal properties.

Example

<?php

class X {
    private string $property = 'abc';
}

echo (new X)->property;

?>

Literal Examples

  • Cannot access private property X::$property

  • Cannot access protected property X::$property

Solutions

  • Relax the visibility constraint to protected or public to make the property available to the calling context.

  • Create a method to access the value of the property.

  • Extends the class and change the visibility level of the property from protected to public. This has no effect for private.

Static Analysis

This error may be tracked down with the following static analysis rules: Classes/AccessProtected.