(PHP 8 >= 8.3.0)
此屬性用於指示一個方法旨在覆寫父類別的方法,或者它實作了介面中定義的方法。
如果在父類別或已實作的介面中不存在具有相同名稱的方法,則會發出編譯時錯誤。
<?php
class Base {
protected function foo(): void {}
}
final class Extended extends Base {
#[\Override]
protected function boo(): void {}
}
?>
以上範例在 PHP 8.3 中的輸出類似於
Fatal error: Extended::boo() has #[\Override] attribute, but no matching parent method exists