The Deprecated attribute

(PHP 8 >= 8.4.0)

Giriş

This attribute is used to mark functionality as deprecated. Using deprecated functionality will cause an E_USER_DEPRECATED error to be emitted.

As of PHP 8.5.0, this attribute can also be applied to traits and to compile-time constants declared outside of a class.

Sınıf Sözdizimi

#[\Attribute(
    \Attribute::TARGET_METHOD
    | \Attribute::TARGET_FUNCTION
    | \Attribute::TARGET_CLASS_CONSTANT
    | \Attribute::TARGET_CONSTANT
    | \Attribute::TARGET_CLASS,
)]

final class Deprecated {
/* Özellikler */
public readonly ?string $message;
public readonly ?string $since;
/* Yöntemler */
public function __construct(?string $message = null, ?string $since = null)
}

Özellikler

message

An optional message explaining the reason for the deprecation and possible replacement functionality. Will be included in the emitted deprecation message.

since

An optional string indicating since when the functionality is deprecated. The contents are not validated by PHP and may contain a version number, a date or any other value that is considered appropriate. Will be included in the emitted deprecation message.

Functionality that is part of PHP will use Major.Minor as the since value, for example '8.4'.

Örnekler

<?php

#[\Deprecated(message: "use safe_replacement() instead", since: "1.5")]
function unsafe_function()
{
   echo "This is unsafe", PHP_EOL;
}

unsafe_function();

?>

Yukarıdaki örneğin PHP 8.4 çıktısı şuna benzer:

Deprecated: Function unsafe_function() is deprecated since 1.5, use safe_replacement() instead in example.php on line 9
This is unsafe

Sürüm Bilgisi

Sürüm: Açıklama
8.5.0 Deprecated can now be applied to traits and to compile-time constants declared outside of a class, the latter through the new Attribute::TARGET_CONSTANT target.

İçindekiler

add a note

User Contributed Notes 1 note

up
-1
miqrogroove at gmail dot com
10 days ago
Here is a bit of wisdom from the Attribute parser:

Fatal error: Attribute "Deprecated" cannot target parameter (allowed targets: function, method, class constant)

<?php
function unsafe_function(
   #[\Deprecated(message: "don't use hello param anymore", since: "1.5")]
   string $hello,
) {
   echo "This is unsafe", PHP_EOL;
}
To Top