php version guide
PHP 8.3 Changes
PHP 8.3 added typed class constants, the #[\Override] attribute, dynamic class-constant fetching, readonly cloning amendments, and json_validate(). These changes are useful for safer maintenance and library code.
Changes Worth Recognising
- Typed class constants make inherited configuration contracts clearer.
#[\Override]catches misspelled or stale overriding methods.json_validate()checks JSON syntax without building a decoded value.- Readonly properties can be reinitialised during cloning from class scope.
Representative Code
PHP example
<?php
interface Formatter
{
public function format(string $value): string;
}
final class UpperFormatter implements Formatter
{
#[\Override]
public function format(string $value): string
{
return strtoupper($value);
}
}
echo (new UpperFormatter())->format('php') . PHP_EOL;
// Prints:
// PHP
Upgrade Review
- Add
#[\Override]where subclasses and implementations deliberately override behaviour. - Use
json_validate()only when syntax validation is enough; decode when the application needs the data. - Review clone behaviour in readonly value objects.
The override attribute is a low-cost maintenance win because it turns accidental method drift into an immediate error.
Practice
Protect an Override
Add #[\Override] to an interface implementation, then deliberately misspell the method name and observe the failure.
Show solution
With #[\Override], PHP reports that the marked method does not override a parent method or implemented interface method.