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 const %s::%s

Description

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

Example

<?php

class X {
    private const A = 1;
}

echo X::A;

?>

Literal Examples

  • Cannot access private constant X::A

Alternatives

  • Relax the visibility constraint to protected or public to make the constant available to the calling context.
  • Create a method to access the value of the constant.
  • Extends the class and change the visibility level of the constant.

In more recent PHP versions, this error message is now :ref:cannot-access-%s-constant-%s::%s.