定義済みのアトリビュート

目次

PHP には、定義済みのアトリビュートがいくつか用意されています。

Attribute クラス

はじめに

アトリビュートを使うと、 コンピューターが解析できる構造化されたメタデータの情報を、 コードの宣言時に埋め込むことができます。 つまり、クラス、メソッド、関数、パラメータ、プロパティ、クラス定数にアトリビュートを指定することができます。 アトリビュートで定義されたメタデータは、 実行時に リフレクションAPI を使って調べることが出来ます。 よって、アトリビュートは、 コードに直接埋め込むことが出来る、 設定のための言語とみなすことができます。

クラス概要

final Attribute
/* 定数 */
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;
/* プロパティ */
public int $flags;
/* メソッド */
public __construct(int $flags = Attribute::TARGET_ALL)

定義済み定数

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

プロパティ

flags

AllowDynamicProperties クラス

はじめに

このアトリビュートを使うと、 クラスに 動的なプロパティ を許可することをマークできます。

クラス概要

final AllowDynamicProperties
/* メソッド */
public __construct()

動的なプロパティは、PHP 8.2.0 以降は推奨されなくなりました。 よって、このアトリビュートでクラスをマークせずに、 動的なプロパティを使うと、 推奨されない警告が発生します。

<?php
class DefaultBehaviour { }

#[
\AllowDynamicProperties]
class 
ClassAllowsDynamicProperties { }

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

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

上の例の PHP 8.2 での出力は、このようになります。:

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

Override クラス

はじめに

クラス概要

final Override
/* メソッド */
public __construct()

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

ReturnTypeWillChange クラス

はじめに

ほとんどの final でない内部メソッドは、 それをオーバライドする際、 互換性がある戻り値の型を宣言することが必須になっています。 そうしない場合、継承が有効かを検証する際に、 推奨されない警告が発生します。 PHP のバージョン間の互換性を保ちたいがために、 戻り値の型を宣言できない場合、 アトリビュート #[\ReturnTypeWillChange] を追加することで警告を抑止できます。

クラス概要

final ReturnTypeWillChange
/* メソッド */
public __construct()

SensitiveParameter クラス

はじめに

このアトリビュートを使うと、スタックトレース中に現れた場合に、 値を削除すべき秘密の値としてパラメータをマークできます。

クラス概要

final SensitiveParameter
/* メソッド */
public __construct()

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

?>

上の例の PHP 8.2 での出力は、たとえば以下のようになります。:

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}

参考