# Class Constant Visibility

> Section in “New and Extended Functionality › Constants” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/constants/class-constant-visibility/>

In PHP 7.1, the concept of `public`, `protected`, and `private` visibility of properties and methods has been expanded to class constants. This change allows for applying the information hiding principle also to constants. It is now possible to hide implementation details such as bitmasks and magic numbers from the user of a class.

The class in the example below declares a private constant named `FOO` and uses its value as the default for a constructor parameter:

``` php
class C
{
    private const FOO = 'bar';

    public function __construct(string $parameter = self::FOO)
    {
        var_dump($parameter);
    }
}

$o = new C;
```

Executing the code shown above will print the output shown below:

    string(3) "bar"

Just like for properties and methods, it is a best practice that a class constant should be declared private unless there is a good reason for it to be protected or public.

---

Previous: [Arrays as Constant Values](https://php7explained.com/new-and-extended-functionality/constants/arrays-as-constant-values/index.html.md) | Next: [Strings](https://php7explained.com/new-and-extended-functionality/strings/index.html.md)
