New “Override” attribute in PHP 8.3

by

Adding the #[\Override] attribute to a method will ensure that a method with the same name exists in a parent class or in an implemented interface. #[\Override]attribute makes it clear that overriding a parent method is purposeful and simplifies refactoring. Also the removal of an overridden parent method will be detected.

use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase {
protected $logFile;

protected function setUp(): void {
$this->logFile = fopen('/tmp/log/file1.log', 'w');
}

#[\Override]
protected function taerDown(): void {
fclose($this->logFile);
unlink('/tmp/log/file1.log');
}
}

// Fatal error: MyTest::taerDown() has #[\Override] attribute,
// but no matching parent method exists

Leave a Reply

Your email address will not be published. Required fields are marked *