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

Constants In Traits

Constants are allowed in traits in PHP 8.3 and more recent. Until then, they were not supported.

PHP code

<?php

trait T {
    const A = 1;
}

class X {
    use T;
}

echo X::A;
?>

Before

PHP Fatal error:  Traits cannot have constants

After

1

PHP version change

This behavior changed in 8.2.

Error Messages

Analyzer

© 2023-2026, Damien Seguy - Exakat