라이브러리

[PHP] ReflectionAttribute::isRepeated - 이 이름의 속성이 코드 요소에서 반복되었는지 여부를 반환합니다.




ReflectionAttribute::isRepeated


ReflectionAttribute::isRepeated는 PHP 8.2 버전부터 사용할 수 있는 기능입니다. 이 기능은 특정 속성에 적용된 어트리뷰트가 반복적으로 적용되었는지 여부를 확인하는 데 사용됩니다.

사용 방법


ReflectionAttribute::isRepeated를 사용하려면 먼저 ReflectionClass 또는 ReflectionProperty를 생성한 후, 해당 클래스 또는 속성에 적용된 어트리뷰트를 가져와야 합니다. 그런 다음, `isRepeated` 메서드를 호출하여 어트리뷰트가 반복적으로 적용되었는지 여부를 확인할 수 있습니다.

예제


#hostingforum.kr
php

use Attribute;

use ReflectionClass;

use ReflectionProperty;



#[Attribute]

class MyAttribute {

    public function __construct(public bool $repeated = false) {}

}



class MyClass {

    #[MyAttribute(repeated: true)]

    public string $myProperty;



    #[MyAttribute(repeated: true)]

    #[MyAttribute]

    public string $myProperty2;

}



$reflectionClass = new ReflectionClass(MyClass::class);

$reflectionProperty = $reflectionClass->getProperty('myProperty');



$attribute = $reflectionProperty->getAttributes()[0];

echo $attribute->getName() . "
"; // MyAttribute

echo $attribute->isRepeated() ? 'true' : 'false'; // true



$reflectionProperty2 = $reflectionClass->getProperty('myProperty2');

$attribute2 = $reflectionProperty2->getAttributes()[0];

echo $attribute2->getName() . "
"; // MyAttribute

echo $attribute2->isRepeated() ? 'true' : 'false'; // true



$attribute3 = $reflectionProperty2->getAttributes()[1];

echo $attribute3->getName() . "
"; // MyAttribute

echo $attribute3->isRepeated() ? 'true' : 'false'; // false



결과


#hostingforum.kr


MyAttribute

true

MyAttribute

true

MyAttribute

false



결론


ReflectionAttribute::isRepeated는 PHP 8.2 버전부터 사용할 수 있는 기능으로, 특정 속성에 적용된 어트리뷰트가 반복적으로 적용되었는지 여부를 확인하는 데 사용됩니다. 이 기능을 사용하여 어트리뷰트의 반복적 적용을 확인할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 292 페이지

검색

게시물 검색