|
Predefined AttributesTable of Contents
PHP provides some predefined attributes that can be used. The Attribute classIntroductionAttributes 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
Properties
See AlsoThe AllowDynamicProperties classIntroductionThis attribute is used to mark classes that allow dynamic properties. Class synopsis
final
AllowDynamicProperties
/* Methods */
public __construct()
ExamplesDynamic 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 Output of the above example in PHP 8.2: Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10 See AlsoThe Override classIntroduction
Class synopsis
final
Override
/* Methods */
public __construct()
Examples
<?php 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 classIntroduction
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 Class synopsis
final
ReturnTypeWillChange
/* Methods */
public __construct()
See AlsoThe SensitiveParameter classIntroductionThis 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 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} |