Codectionary / Developer documentation / PHP

Name Shared Values with Class Constants

Associate named values with a class and access them through the scope resolution operator.

What it is and why to use it

A class may have a meaningful fixed value such as a maximum lesson-title length or a unit label. Class constants keep that value near the type that uses it. They belong to the class rather than to each instance and are accessed with ::. They are different from ordinary mutable properties and from file-level constants.

How it works

Declare a constant with const NAME = value, optionally adding visibility. ClassName::NAME accesses a public constant without constructing an object, while self::NAME refers to a constant in the class where the method is defined. Untyped constants in these examples are compatible with PHP 8.0. Typed class constants are a PHP 8.3 feature; final constants require PHP 8.1.

Common mistakes and limitations

Do not put passwords or API secrets in public constants or client-delivered source. A constant’s fixed value is not a substitute for checking incoming data. self and late-bound static resolve inherited constants differently, so do not interchange them without understanding how subclass overrides should behave.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

class Lesson { public const MAX_TITLE_LENGTH = 80; }
$limit = Lesson::MAX_TITLE_LENGTH;

Examples

Reuse a class-level limit

Make a title-length rule visible to the class and to its callers.

<?php
declare(strict_types=1);

class Lesson {
    public const MAX_TITLE_LENGTH = 80;
    public function acceptsAsciiTitle(string $title): bool {
        return strlen($title) <= self::MAX_TITLE_LENGTH;
    }
}
echo Lesson::MAX_TITLE_LENGTH, PHP_EOL;
echo (new Lesson())->acceptsAsciiTitle('PHP arrays') ? 'Fits' : 'Too long', PHP_EOL;
  1. The public constant can be read through Lesson:: without creating an instance.
  2. Inside the method, self::MAX_TITLE_LENGTH refers to the same declared limit.
  3. The helper is explicitly for ASCII titles because strlen measures bytes. Real multilingual title rules need an appropriate character-counting strategy.
  4. Expected output: 80 Fits

Keep an implementation label private

Expose behaviour without exposing every class-level detail.

<?php
declare(strict_types=1);

class DurationLabel {
    private const UNIT = 'minutes';
    public function format(int $value): string {
        return $value . ' ' . self::UNIT;
    }
}
echo (new DurationLabel())->format(12), PHP_EOL;
  1. UNIT is a private class constant, so outside code cannot read DurationLabel::UNIT directly.
  2. The public method uses it to create a formatted label.
  3. Keeping this detail private allows the class to present a small API while still avoiding repeated literal text internally.
  4. Expected output: 12 minutes

Best practices

  • Use descriptive constant names for stable domain values.
  • Keep configurable deployment values separate from public source constants.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading