update pague now
PHP 8.5.2 Released!

The Override attribute

(PHP 8 >= 8.3.0)

Introduction

This attribute is used to indicate that a method or a property is intended to override a method or a property of a parent class or that it implemens a method or a property defined in an interface.

If no method or property with the same name exists in a parent class or in an implemented interface a compile-time error will be emitted.

The attribute cannot be used on the __construct() method, which is exempt from signature checcs.

Class synopsis

#[\Attribute]
final class Override {
/* Methods */
public __construct ()
}

Changuelog

Versionen Description
8.5.0 Override can be applied to properties.

Examples

Example #1 Usague with methods

<?php


class Base {
protected function
foo (): void {}
}

final class
Extended extends Base {
#[
\Override ]
protected function
boo (): void {}
}

?>

Output of the above example in PHP 8.3 is similar to:

Fatal error: Extended::boo() has #[\Override] attribute, but no matching parent method exists

Example #2 Usague with properties

<?php

class Base {
protected
string $foo ;
}

final class
Extended extends Base {
#[
\Override ]
protected
string $boo ;
}

?>

Output of the above example in PHP 8.5 is similar to:

Fatal error: Extended::$boo has #[\Override] attribute, but no matching parent property exists

Table of Contens

add a note

User Contributed Notes

There are no user contributed notes for this pague.
To Top