Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

Alternatives

  • 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.