Predefined Attributes

Table of Contents

PHP provides some predefined attributes that can be used.

The Attribute class

Introduction

Attributes offer the ability to add structured, machine-readable metadata information on declarations in code: Classes, methods, functions, parameters, properties and class constants can be the target of an attribute. The metadata defined by attributes can then be inspected at runtime using the Reflection APIs. Attributes could therefore be thought of as a configuration language embedded directly into code.

Class synopsis

final Attribute
/* Constants */
const int Attribute::TARGET_CLASS;
const int Attribute::TARGET_FUNCTION;
const int Attribute::TARGET_METHOD;
const int Attribute::TARGET_PROPERTY;
const int Attribute::TARGET_CLASS_CONSTANT;
const int Attribute::TARGET_PARAMETER;
const int Attribute::TARGET_ALL;
const int Attribute::IS_REPEATABLE;
/* Properties */
public int $flags;
/* Methods */
public __construct(int $flags = Attribute::TARGET_ALL)

Predefined Constants

Attribute::TARGET_CLASS

Attribute::TARGET_FUNCTION

Attribute::TARGET_METHOD

Attribute::TARGET_PROPERTY

Attribute::TARGET_CLASS_CONSTANT

Attribute::TARGET_PARAMETER

Attribute::TARGET_ALL

Attribute::IS_REPEATABLE

Properties

flags

The AllowDynamicProperties class

Introduction

This attribute is used to mark classes that allow dynamic properties.

Class synopsis

final AllowDynamicProperties
/* Methods */
public __construct()

Examples

Dynamic properties are deprecated as of PHP 8.2.0, thus using them without marking the class with this attribute will emit a deprecation notice.

<?php
class DefaultBehaviour { }

#[
\AllowDynamicProperties]
class 
ClassAllowsDynamicProperties { }

$o1 = new DefaultBehaviour();
$o2 = new ClassAllowsDynamicProperties();

$o1->nonExistingProp true;
$o2->nonExistingProp true;
?>

Output of the above example in PHP 8.2:

Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10

The Override class

Introduction

Class synopsis

final Override
/* Methods */
public __construct()

Examples

<?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

The ReturnTypeWillChange class

Introduction

Most non-final internal methods now require overriding methods to declare a compatible return type, otherwise a deprecated notice is emitted during inheritance validation. In case the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, a #[\ReturnTypeWillChange] attribute can be added to silence the deprecation notice.

Class synopsis

final ReturnTypeWillChange
/* Methods */
public __construct()

The SensitiveParameter class

Introduction

This attribute is used to mark a parameter that is sensitive and should have its value redacted if present in a stack trace.

Class synopsis

final SensitiveParameter
/* Methods */
public __construct()

Examples

<?php

function defaultBehavior(
    
string $secret,
    
string $normal
) {
    throw new 
Exception('Error!');
}

function 
sensitiveParametersWithAttribute(
    #[
\SensitiveParameter]
    
string $secret,
    
string $normal
) {
    throw new 
Exception('Error!');
}

try {
    
defaultBehavior('password''normal');
} catch (
Exception $e) {
    echo 
$ePHP_EOLPHP_EOL;
}

try {
    
sensitiveParametersWithAttribute('password''normal');
} catch (
Exception $e) {
    echo 
$ePHP_EOLPHP_EOL;
}

?>

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

Exception: Error! in example.php:7
Stack trace:
#0 example.php(19): defaultBehavior('password', 'normal')
#1 {main}

Exception: Error! in example.php:15
Stack trace:
#0 example.php(25): sensitiveParametersWithAttribute(Object(SensitiveParameterValue), 'normal')
#1 {main}

See Also