PHP Reflection API에 대해 자세히 알아보세요!

青灯夜游
풀어 주다: 2023-04-09 10:52:01
앞으로
1883명이 탐색했습니다.

PHP Reflection API에 대해 자세히 알아보세요!

PHP의 리플렉션 API는 Java의 java.lang.reflect 패키지와 같습니다. 이는 속성, 메서드 및 클래스를 분석할 수 있는 기본 제공 클래스 집합으로 구성됩니다. get_class_vars()와 같은 객체 함수와 어떤 면에서는 유사하지만 더 유연하고 더 많은 정보를 제공할 수 있습니다.

Reflection API는 액세스 제어, 인터페이스 및 추상 클래스와 같은 PHP의 최신 객체 지향 기능과도 작동합니다. 이전 클래스 함수는 이러한 새로운 기능과 함께 사용하기가 덜 쉽습니다. 프레임워크의 소스 코드를 읽은 친구는 의존성 주입, 개체 풀링, 클래스 로딩, 일부 디자인 패턴 등과 같은 PHP의 반사 메커니즘을 어느 정도 이해하고 있어야 하며 모두 반사 메커니즘을 사용합니다.

Reflection API의 일부 클래스

Reflection API의 이러한 클래스를 사용하면 런타임 시 개체, 함수 및 스크립트의 확장된 정보에 액세스할 수 있습니다. 이 정보는 클래스를 분석하거나 프레임워크를 구축하는 데 사용될 수 있습니다.

Class 설명
Reflection 정적 함수 내보내기()
ReflectionClass 클래스 정보 및 도구 제공
ReflectionMethod 수업 방법 정보 및 도구
ReflectionParameter 메서드 매개변수 정보
ReflectionProperty 클래스 속성 정보
ReflectionFunction 함수 정보 및 도구
ReflectionExtension PHP 확장 정보
ReflectionException Error 클래스

클래스 정보 가져오기

작업에서 클래스 속성을 확인하기 위해 get_class_methods, getProduct 등과 같은 몇 가지 함수를 사용했습니다. 이러한 방법은 자세한 수업 정보를 얻는 데 상당한 제한이 있습니다.

Reflection API 클래스에서 제공하는 정적 메서드 내보내기를 통해 클래스 관련 정보를 얻을 수 있습니다. Reflection 및 ReflectionClass 내보내기는 속성 및 메서드의 액세스 제어 상태, 각 메서드에 필요한 매개변수를 포함하여 클래스에 대한 거의 모든 정보를 제공할 수 있습니다. 및 스크립트 문서에서 각 메소드의 위치. 이 두 도구 클래스의 정적 내보내기 방법의 출력 결과는 동일하지만 사용 방법이 다릅니다.

먼저 간단한 클래스를 만듭니다

<?php

class Student {
    public    $name;
    protected $age;
    private   $sex;

    public function __construct($name, $age, $sex)
    {
        $this->setName($name);
        $this->setAge($age);
        $this->setSex($sex);
    }

    public function setName($name)
    {
       $this->name = $name;
    }

    protected function setAge($age)
    {
        $this->age = $age;
    }

    private function setSex($sex)
    {
        $this->sex = $sex;
    }
}
로그인 후 복사

ReflectionClass::export()를 사용하여 클래스 정보를 가져옵니다

ReflectionClass::export(&#39;Student&#39;);
로그인 후 복사

결과 인쇄:

Class [ class Student ] {
    @@ D:\wamp\www\test2.php 3-29
    - Constants [0] { }
    - Static properties [0] { }
    - Static methods [0] { }
    - Properties [3] {
        Property [ public $name ]
        Property [ protected $age ]
        Property [ private $sex ]
    }
    - Methods [4] {
        Method [ public method __construct ] {
            @@ D:\wamp\www\test2.php 8 - 13
            - Parameters [3] {
                Parameter #0 [ $name ]
                Parameter #1 [ $age ]
                Parameter #2 [ $sex ]
            }
        }
        Method [ public method setName ] {
            @@ D:\wamp\www\test2.php 15 - 18
            - Parameters [1] {
                Parameter #0 [ $name ]
            }
        }
        Method [ protected method setAge ] {
            @@ D:\wamp\www\test2.php 20 - 23
            - Parameters [1] {
                Parameter #0 [ $age ]
            }
        }
        Method [ private method setSex ] {
            @@ D:\wamp\www\test2.php 25 - 28
            - Parameters [1] {
                Parameter #0 [ $sex ]
            }
        }
    }
}
로그인 후 복사

ReflectionClass 클래스는 다음과 같이 다양한 도구 메서드를 제공합니다. 공식 매뉴얼 목록은 다음과 같습니다.

ReflectionClass::__construct — 初始化 ReflectionClass 类
ReflectionClass::export — 导出一个类
ReflectionClass::getConstant — 获取定义过的一个常量
ReflectionClass::getConstants — 获取一组常量
ReflectionClass::getConstructor — 获取类的构造函数
ReflectionClass::getDefaultProperties — 获取默认属性
ReflectionClass::getDocComment — 获取文档注释
ReflectionClass::getEndLine — 获取最后一行的行数
ReflectionClass::getExtension — 根据已定义的类获取所在扩展的 ReflectionExtension 对象
ReflectionClass::getExtensionName — 获取定义的类所在的扩展的名称
ReflectionClass::getFileName — 获取定义类的文件名
ReflectionClass::getInterfaceNames — 获取接口(interface)名称
ReflectionClass::getInterfaces — 获取接口
ReflectionClass::getMethod — 获取一个类方法的 ReflectionMethod。
ReflectionClass::getMethods — 获取方法的数组
ReflectionClass::getModifiers — 获取类的修饰符
ReflectionClass::getName — 获取类名
ReflectionClass::getNamespaceName — 获取命名空间的名称
ReflectionClass::getParentClass — 获取父类
ReflectionClass::getProperties — 获取一组属性
ReflectionClass::getProperty — 获取类的一个属性的 ReflectionProperty
ReflectionClass::getReflectionConstant — Gets a ReflectionClassConstant for a class&#39;s constant
ReflectionClass::getReflectionConstants — Gets class constants
ReflectionClass::getShortName — 获取短名
ReflectionClass::getStartLine — 获取起始行号
ReflectionClass::getStaticProperties — 获取静态(static)属性
ReflectionClass::getStaticPropertyValue — 获取静态(static)属性的值
ReflectionClass::getTraitAliases — 返回 trait 别名的一个数组
ReflectionClass::getTraitNames — 返回这个类所使用 traits 的名称的数组
ReflectionClass::getTraits — 返回这个类所使用的 traits 数组
ReflectionClass::hasConstant — 检查常量是否已经定义
ReflectionClass::hasMethod — 检查方法是否已定义
ReflectionClass::hasProperty — 检查属性是否已定义
ReflectionClass::implementsInterface — 接口的实现
ReflectionClass::inNamespace — 检查是否位于命名空间中
ReflectionClass::isAbstract — 检查类是否是抽象类(abstract)
ReflectionClass::isAnonymous — 检查类是否是匿名类
ReflectionClass::isCloneable — 返回了一个类是否可复制
ReflectionClass::isFinal — 检查类是否声明为 final
ReflectionClass::isInstance — 检查类的实例
ReflectionClass::isInstantiable — 检查类是否可实例化
ReflectionClass::isInterface — 检查类是否是一个接口(interface)
ReflectionClass::isInternal — 检查类是否由扩展或核心在内部定义
ReflectionClass::isIterateable — 检查是否可迭代(iterateable)
ReflectionClass::isSubclassOf — 检查是否为一个子类
ReflectionClass::isTrait — 返回了是否为一个 trait
ReflectionClass::isUserDefined — 检查是否由用户定义的
ReflectionClass::newInstance — 从指定的参数创建一个新的类实例
ReflectionClass::newInstanceArgs — 从给出的参数创建一个新的类实例。
ReflectionClass::newInstanceWithoutConstructor — 创建一个新的类实例而不调用它的构造函数
ReflectionClass::setStaticPropertyValue — 设置静态属性的值
ReflectionClass::__toString — 返回 ReflectionClass 对象字符串的表示形式。
로그인 후 복사

Reflection::export()를 사용하여 클래스 정보 얻기

$prodClass = new ReflectionClass(&#39;Student&#39;);
Reflection::export($prodClass);
로그인 후 복사

결과 인쇄

Class [ class Student ] {
    @@ D:\wamp\www\test2.php 3-29
    - Constants [0] { }
    - Static properties [0] { }
    - Static methods [0] { }
    - Properties [3] {
        Property [ public $name ]
        Property [ protected $age ]
        Property [ private $sex ]
    }
    - Methods [4] {
        Method [ public method __construct ] {
            @@ D:\wamp\www\test2.php 8 - 13
            - Parameters [3] {
                Parameter #0 [ $name ]
                Parameter #1 [ $age ]
                Parameter #2 [ $sex ]
            }
        }
        Method [ public method setName ] {
            @@ D:\wamp\www\test2.php 15 - 18
            - Parameters [1] {
                Parameter #0 [ $name ]
            }
        }
        Method [ protected method setAge ] {
            @@ D:\wamp\www\test2.php 20 - 23
            - Parameters [1] {
                Parameter #0 [ $age ]
            }
        }
        Method [ private method setSex ] {
            @@ D:\wamp\www\test2.php 25 - 28
            - Parameters [1] {
                Parameter #0 [ $sex ]
            }
        }
    }
}
로그인 후 복사

ReflectionClass 객체를 생성한 후 Reflection 도구 클래스를 사용하여 출력할 수 있습니다. 학생 수업에 대한 관련 정보입니다. Reflection::export()는 Reflector 인터페이스를 구현하는 모든 클래스의 인스턴스를 형식화하고 내보낼 수 있습니다.

클래스 확인

앞서 ReflectionClass 도구 클래스에 대해 배웠습니다. 이 클래스는 클래스 정보를 얻기 위한 많은 도구 메서드를 제공합니다. 예를 들어, Student 클래스의 유형과 인스턴스화 가능 여부를 얻을 수 있습니다.

도구 기능

function classData(ReflectionClass $class) {
    $details = &#39;&#39;;
    $name = $class->getName();          // 返回要检查的类名
    if ($class->isUserDefined()) {      // 检查类是否由用户定义
        $details .= "$name is user defined" . PHP_EOL;
    }
    if ($class->isInternal()) {         // 检查类是否由扩展或核心在内部定义
        $details .= "$name is built-in" . PHP_EOL;
    }
    if ($class->isInterface()) {        // 检查类是否是一个接口
        $details .= "$name is interface" . PHP_EOL;
    }
    if ($class->isAbstract()) {         // 检查类是否是抽象类
        $details .= "$name is an abstract class" . PHP_EOL;
    }
    if ($class->isFinal()) {            // 检查类是否声明为 final
        $details .= "$name is a final class" . PHP_EOL;
    }
    if ($class->isInstantiable()) {     // 检查类是否可实例化
        $details .= "$name can be instantiated" . PHP_EOL;
    } else {
        $details .= "$name can not be instantiated" . PHP_EOL;
    }
    return $details;
}

$prodClass = new ReflectionClass(&#39;Student&#39;);
print classData($prodClass);
로그인 후 복사

결과 인쇄

Student is user defined
Student can be instantiated
로그인 후 복사

클래스 관련 정보를 얻는 것 외에도 파일 이름과 파일 위치도 얻을 수 있습니다. ReflectionClass 객체가 제공하는 사용자 정의 클래스 클래스의 시작 및 끝 줄과 같은 관련 소스 코드 정보.

function getClassSource(ReflectionClass $class) {
    $path  = $class->getFileName();  // 获取类文件的绝对路径
    $lines = @file($path);           // 获得由文件中所有行组成的数组
    $from  = $class->getStartLine(); // 提供类的起始行
    $to    = $class->getEndLine();   // 提供类的终止行
    $len   = $to - $from + 1;
    return implode(array_slice($lines, $from - 1, $len));
}

$prodClass = new ReflectionClass(&#39;Student&#39;);
var_dump(getClassSource($prodClass));
로그인 후 복사

Print results

string &#39;class Student {
    public    $name;
    protected $age;
    private   $sex;

    public function __construct($name, $age, $sex)
    {
        $this->setName($name);
        $this->setAge($age);
        $this->setSex($sex);
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    protected function setAge($age)
    {
        $this->age = $age;
    }

    private function setSex($sex)
    {
        $this->sex = $sex;
    }
}
&#39; (length=486)
로그인 후 복사

getClassSource가 ReflectionClass 객체를 매개변수로 받아들이고 해당 클래스의 소스 코드를 반환하는 것을 볼 수 있습니다. 이 함수는 오류 처리를 무시합니다. 실제로 매개변수와 결과 코드를 확인해야 합니다!

메서드 확인

클래스 확인과 마찬가지로 ReflectionMethod 개체를 사용하여 클래스의 메서드를 확인할 수 있습니다.

ReflectionMethod 객체를 얻는 방법에는 두 가지가 있습니다.

첫 번째는 ReflectionClass::getMethods()를 통해 ReflectionMethod 객체의 배열을 얻는 것입니다. 이 메소드의 장점은 메소드 이름을 미리 알 필요가 없다는 것입니다. 클래스에 있는 모든 메서드의 ReflectionMethod를 반환합니다.

두 번째 방법은 ReflectionMethod 클래스를 직접 사용하여 객체를 인스턴스화하는 것입니다. 이 방법은 하나의 클래스 메소드 객체만 얻을 수 있으며, 메소드 이름을 미리 알아야 합니다.

ReflectionMethod 객체의 유틸리티 메서드:

ReflectionMethod::__construct — ReflectionMethod의 생성자
ReflectionMethod::export — 콜백 메서드 내보내기
ReflectionMethod::getClosure — 동적으로 생성된 메서드 호출 인터페이스를 반환합니다. 번역기 참고: 예 이 반환 값을 사용하세요. 비공개 메서드를 직접 호출합니다.
ReflectionMethod::getDeclaringClass — 리플렉션 함수 호출 매개변수의 클래스 표현을 가져옵니다.
ReflectionMethod::getModifiers — 메서드의 수정자를 가져옵니다.
ReflectionMethod::getPrototype — 메서드 프로토타입 반환(있는 경우)
ReflectionMethod::invoke — 호출
ReflectionMethod::invokeArgs — 매개변수를 사용하여 실행
ReflectionMethod::isAbstract — 메서드가 추상 메서드인지 확인
ReflectionMethod::isConstructor — 메서드가 생성자 메서드인지 확인
ReflectionMethod::isDestructor — 메서드가 소멸자 메서드인지 확인
ReflectionMethod::isFinal — 메서드가 소멸자인지 확인 최종 정의
ReflectionMethod::isPrivate — 메서드가 비공개 메서드인지 확인
ReflectionMethod::isProtected — 메서드가 보호된 메서드인지 확인(protected)
ReflectionMethod:: isPublic — 메서드가 공용 메서드인지 확인합니다.
ReflectionMethod::isStatic — 메서드가 공용인지 확인합니다. 정적 메서드입니다.
ReflectionMethod::setAccessible — 메서드에 액세스할 수 있는지 여부를 설정합니다.
ReflectionMethod::__toString — 메서드의 문자열 표현식을 반환합니다. 반사 메소드 객체

ReflectionClass::getMethods()

ReflectionClass::getMethods( )를 전달할 수 있습니다. ReflectionMethod 객체의 배열을 가져옵니다.

$prodClass = new ReflectionClass(&#39;Student&#39;);
$methods = $prodClass->getMethods();
var_dump($methods);
로그인 후 복사

결과 인쇄

array (size=4)
  0 => &
    object(ReflectionMethod)[2]
      public &#39;name&#39; => string &#39;__construct&#39; (length=11)
      public &#39;class&#39; => string &#39;Student&#39; (length=7)
  1 => &
    object(ReflectionMethod)[3]
      public &#39;name&#39; => string &#39;setName&#39; (length=7)
      public &#39;class&#39; => string &#39;Student&#39; (length=7)
  2 => &
    object(ReflectionMethod)[4]
      public &#39;name&#39; => string &#39;setAge&#39; (length=6)
      public &#39;class&#39; => string &#39;Student&#39; (length=7)
  3 => &
    object(ReflectionMethod)[5]
      public &#39;name&#39; => string &#39;setSex&#39; (length=6)
      public &#39;class&#39; => string &#39;Student&#39; (length=7)
로그인 후 복사

Student의 ReflectionMethod 객체 배열을 얻은 것을 볼 수 있습니다. 각 요소는 두 개의 공개 속성을 갖는 객체이며, name은 메소드 이름이고 class는 해당 객체가 속한 클래스입니다. 메소드 정보를 얻기 위해 객체 메소드를 호출할 수 있습니다.

ReflectionMethod

ReflectionMethod 클래스를 직접 사용하여 클래스 메서드에 대한 정보 얻기

$method = new ReflectionMethod(&#39;Student&#39;, &#39;setName&#39;);
var_dump($method);
로그인 후 복사

결과 인쇄

object(ReflectionMethod)[1]
  public &#39;name&#39; => string &#39;setName&#39; (length=7)
  public &#39;class&#39; => string &#39;Student&#39; (length=7)
로그인 후 복사

주의

在PHP5中,如果被检查的方法只返回对象(即使对象是通过引用赋值或传递的),那么 ReflectionMethod::retursReference() 不会返回 true。只有当被检测的方法已经被明确声明返回引用(在方法名前面有&符号)时,ReflectionMethod::returnsReference() 才返回 true。

检查方法参数

在PHP5中,声明类方法时可以限制参数中对象的类型,因此检查方法的参数变得非常必要。

类似于检查方法,ReflectionParameter 对象可以用于检查类中的方法,该对象可以告诉你参数的名称,变量是否可以按引用传递,还可以告诉你参数类型提示和方法是否接受空值作为参数。

获得 ReflectionParameter 对象的方法有同样两种,这和获取 ReflectionMethod 对象非常类似:

第一种是通过 ReflectionMethod::getParameters() 方法返回 ReflectionParameter 对象数组,这种方法可以获取到一个方法的全部参数对象。

第二种是直接使用 ReflectionParameter 类实例化获取对象,这种方法只能获取到单一参数的对象。

ReflectionParameter 对象的工具方法:

ReflectionParameter::allowsNull — Checks if null is allowed
ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value
ReflectionParameter::__clone — Clone
ReflectionParameter::__construct — Construct
ReflectionParameter::export — Exports
ReflectionParameter::getClass — Get the type hinted class
ReflectionParameter::getDeclaringClass — Gets declaring class
ReflectionParameter::getDeclaringFunction — Gets declaring function
ReflectionParameter::getDefaultValue — Gets default parameter value
ReflectionParameter::getDefaultValueConstantName — Returns the default value&#39;s constant name if default value is constant or null
ReflectionParameter::getName — Gets parameter name
ReflectionParameter::getPosition — Gets parameter position
ReflectionParameter::getType — Gets a parameter&#39;s type
ReflectionParameter::hasType — Checks if parameter has a type
ReflectionParameter::isArray — Checks if parameter expects an array
ReflectionParameter::isCallable — Returns whether parameter MUST be callable
ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available
ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is constant
ReflectionParameter::isOptional — Checks if optional
ReflectionParameter::isPassedByReference — Checks if passed by reference
ReflectionParameter::isVariadic — Checks if the parameter is variadic
ReflectionParameter::__toString — To string
로그인 후 복사

ReflectionMethod::getParameters()

同获取方法,此方法会返回一个数组,包含方法每个参数的 ReflectionParameter 对象

$method = new ReflectionMethod(&#39;Student&#39;, &#39;setName&#39;);
$params = $method->getParameters();
var_dump($params);
로그인 후 복사

打印结果

array (size=1)
  0 => &
    object(ReflectionParameter)[2]
      public &#39;name&#39; => string &#39;name&#39; (length=4)
로그인 후 복사

ReflectionParameter

我们来了解一下这种方式,为了更好的理解,我修改一下 Student 类的 setName方法,增加两个参数 a, b

...
    public function setName($name, $a, $b)
    {
        $this->name = $name;
    }
...
로그인 후 복사

首先我们看一下 ReflectionParameter 类的构造方法

public ReflectionParameter::__construct ( string $function , string $parameter )
로그인 후 복사

可以看到该类实例化时接收两个参数:

$function:当需要获取函数为公共函数时只需传函数名称即可。当该函数是某个类方法时,需要传递一个数组,格式为:array('class', 'function')。

$parameter:这个参数可以传递两种,第一种为参数名(无$符号),第二种为参数索引。注意:无论是参数名还是索引,该参数都必须存在,否则会报错。

下面举例:

$params = new ReflectionParameter(array(&#39;Student&#39;, &#39;setName&#39;), 1);
var_dump($params);
로그인 후 복사

打印结果

object(ReflectionParameter)[1]
  public &#39;name&#39; => string &#39;a&#39; (length=1)
로그인 후 복사

我们再定义一个函数测试一下

function foo($a, $b, $c) { }
$reflect = new ReflectionParameter(&#39;foo&#39;, &#39;c&#39;);
var_dump($reflect);
로그인 후 복사

打印结果

object(ReflectionParameter)[2]
  public &#39;name&#39; => string &#39;c&#39; (length=1)
로그인 후 복사

结语

php的反射API功能非常的强大,它可以将一个类的详细信息获取出来。我们可以通过反射API编写个类来动态调用Module对象,该类可以自由加载第三方插件并集成进已有的系统。而不需要把第三方的代码硬编码进原有的代码中。虽然实际开发中使用反射情况比较少,但了解反射API对工作中对代码结构的了解和开发业务模式帮助还是非常大的。此篇博文断断续续的写了很久(主要就是懒!),如有错误与不足欢迎指正,建议!!

相关教程推荐:《PHP教程

위 내용은 PHP Reflection API에 대해 자세히 알아보세요!의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:cnblogs.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!