Computer >> 컴퓨터 >  >> 프로그램 작성 >> PHP

PHP 8에서 Reflection API로 속성 읽기

<시간/>

PHP 8에서는 클래스, 속성 및 클래스 상수, 메서드, 함수, 매개변수를 사용하여 속성에 액세스합니다.

PHP 8의 리플렉션 API getAttribute() 전달 일치하는 모든 Reflection 개체에 대한 메서드입니다.

getAttribute() 메소드는 ReflectionAttribute 배열을 반환합니다. 속성 이름, 인수를 요청하고 의미 있는 속성의 인스턴스를 인스턴스화할 수 있는 그림.

예 - PHP 8에서 Reflection API로 속성 읽기

<?php
   #[Reading]
   #[Property(type: 'function', name: 'Student')]
   function Student()
   {
      return "Student";
   }
   function getAttributes(Reflector $reflection)
   {
      $attributes = $reflection->getAttributes();
      $finalresult = [];
      foreach ($attributes as $attribute)
      {
         $finalresult[$attribute->getName() ] = $attribute->getArguments();
      }
      return $finalresult;
   }
   $reflection = new ReflectionFunction("Student");
   print_r(getAttributes($reflection));
?>

출력

Array
(
   [Reading] => Array
   (
   )

   [Property] => Array
   (
      [type] => function
      [name] => Student
   )
)