백엔드 개발 PHP 튜토리얼 PHP의 Reflection API에 대한 자세한 설명

PHP의 Reflection API에 대한 자세한 설명

Dec 27, 2017 am 09:56 AM
api php reflection

이 글에서는 주로 PHP Reflection API를 소개하며 Reflection 클래스, ReflectionException 클래스, ReflectionFunction 클래스, ReflectionParameter 클래스, ReflectionClass 클래스, ReflectionMethod 클래스 등에 대해 설명합니다. 그것이 모두에게 도움이 되기를 바랍니다.

PHP Reflection API는 PHP5에서만 사용할 수 있는 새로운 기능으로, 주석을 포함하여 클래스, 메서드, 속성, 매개변수 등에 대한 자세한 정보를 내보내거나 추출하는 데 사용됩니다.

PHP Reflection API에는 다음이 포함됩니다.

class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
로그인 후 복사

특정 API 설명:

①Reflection 클래스

<?php
class Reflection
{
  public static mixed export(Reflector r [,bool return])
  //导出一个类或方法的详细信息
  public static array getModifierNames(int modifiers)
  //取得修饰符的名字
}
?>
로그인 후 복사

②ReflectionException 클래스

이 클래스는 표준 클래스를 상속하며 특별한 메소드가 없습니다. 또는 속성.

3ReflectionFunction 클래스

<?php
class ReflectionFunction implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //导出该函数的详细信息
  public string getName()
  //取得函数名
  public bool isInternal()
  //测试是否为系统内部函数
  public bool isUserDefined()
  //测试是否为用户自定义函数
  public string getFileName()
  //取得文件名,包括路径名
  public int getStartLine()
  //取得定义函数的起始行
  public int getEndLine()
  //取得定义函数的结束行
  public string getDocComment()
  //取得函数的注释
  public array getStaticVariables()
  //取得静态变量
  public mixed invoke(mixed* args)
  //调用该函数,通过参数列表传参数
  public mixed invokeArgs(array args)
  //调用该函数,通过数组传参数
  public bool returnsReference()
  //测试该函数是否返回引用
  public ReflectionParameter[] getParameters()
  //取得该方法所需的参数,返回值为对象数组
  public int getNumberOfParameters()
  //取得该方法所需的参数个数
  public int getNumberOfRequiredParameters()
  //取得该方法所需的参数个数
}
?>
로그인 후 복사

4ReflectionParameter 클래스:

<?php
class ReflectionParameter implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //导出该参数的详细信息
  public string getName()
  //取得参数名
  public bool isPassedByReference()
  //测试该参数是否通过引用传递参数
  public ReflectionClass getClass()
  //若该参数为对象,返回该对象的类名
  public bool isArray()
  //测试该参数是否为数组类型
  public bool allowsNull()
  //测试该参数是否允许为空
  public bool isOptional()
  //测试该参数是否为可选的,当有默认参数时可选
  public bool isDefaultValueAvailable()
  //测试该参数是否为默认参数
  public mixed getDefaultValue()
  //取得该参数的默认值
}
?>
로그인 후 복사

⑤ReflectionClass 클래스:

<?php
class ReflectionClass implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export()
  //导出该类的详细信息
  public string getName()
  //取得类名或接口名
  public bool isInternal()
  //测试该类是否为系统内部类
  public bool isUserDefined()
  //测试该类是否为用户自定义类
  public bool isInstantiable()
  //测试该类是否被实例化过
  public bool hasConstant(string name)
  //测试该类是否有特定的常量
  public bool hasMethod(string name)
  //测试该类是否有特定的方法
  public bool hasProperty(string name)
  //测试该类是否有特定的属性
  public string getFileName()
  //取得定义该类的文件名,包括路径名
  public int getStartLine()
  //取得定义该类的开始行
  public int getEndLine()
  //取得定义该类的结束行
  public string getDocComment()
  //取得该类的注释
  public ReflectionMethod getConstructor()
  //取得该类的构造函数信息
  public ReflectionMethod getMethod(string name)
  //取得该类的某个特定的方法信息
  public ReflectionMethod[] getMethods()
  //取得该类的所有的方法信息
  public ReflectionProperty getProperty(string name)
  //取得某个特定的属性信息
  public ReflectionProperty[] getProperties()
  //取得该类的所有属性信息
  public array getConstants()
  //取得该类所有常量信息
  public mixed getConstant(string name)
  //取得该类特定常量信息
  public ReflectionClass[] getInterfaces()
  //取得接口类信息
  public bool isInterface()
  //测试该类是否为接口
  public bool isAbstract()
  //测试该类是否为抽象类
  public bool isFinal()
  //测试该类是否声明为final
  public int getModifiers()
  //取得该类的修饰符,返回值类型可能是个资源类型
  //通过Reflection::getModifierNames($class->getModifiers())进一步读取
  public bool isInstance(stdclass object)
  //测试传入的对象是否为该类的一个实例
  public stdclass newInstance(mixed* args)
  //创建该类实例
  public ReflectionClass getParentClass()
  //取得父类
  public bool isSubclassOf(ReflectionClass class)
  //测试传入的类是否为该类的父类
  public array getStaticProperties()
  //取得该类的所有静态属性
  public mixed getStaticPropertyValue(string name [, mixed default])
  //取得该类的静态属性值,若private,则不可访问
  public void setStaticPropertyValue(string name, mixed value)
  //设置该类的静态属性值,若private,则不可访问,有悖封装原则
  public array getDefaultProperties()
  //取得该类的属性信息,不含静态属性
  public bool isIterateable()
  public bool implementsInterface(string name)
  //测试是否实现了某个特定接口
  public ReflectionExtension getExtension()
  public string getExtensionName()
}
?>
로그인 후 복사

⑥ReflectionMethod 클래스:

<?php
class ReflectionMethod extends ReflectionFunction
{
  public __construct(mixed class, string name)
  public string __toString()
  public static string export()
  //导出该方法的信息
  public mixed invoke(stdclass object, mixed* args)
  //调用该方法
  public mixed invokeArgs(stdclass object, array args)
  //调用该方法,传多参数
  public bool isFinal()
  //测试该方法是否为final
  public bool isAbstract()
  //测试该方法是否为abstract
  public bool isPublic()
  //测试该方法是否为public
  public bool isPrivate()
  //测试该方法是否为private
  public bool isProtected()
  //测试该方法是否为protected
  public bool isStatic()
  //测试该方法是否为static
  public bool isConstructor()
  //测试该方法是否为构造函数
  public bool isDestructor()
  //测试该方法是否为析构函数
  public int getModifiers()
  //取得该方法的修饰符
  public ReflectionClass getDeclaringClass()
  //取得该方法所属的类
  // Inherited from ReflectionFunction
  final private __clone()
  public string getName()
  public bool isInternal()
  public bool isUserDefined()
  public string getFileName()
  public int getStartLine()
  public int getEndLine()
  public string getDocComment()
  public array getStaticVariables()
  public bool returnsReference()
  public ReflectionParameter[] getParameters()
  public int getNumberOfParameters()
  public int getNumberOfRequiredParameters()
}
?>
로그인 후 복사

7ReflectionProperty 클래스:

<?php
class ReflectionProperty implements Reflector
{
  final private __clone()
  public __construct(mixed class, string name)
  public string __toString()
  public static string export()
  //导出该属性的详细信息
  public string getName()
  //取得该属性名
  public bool isPublic()
  //测试该属性名是否为public
  public bool isPrivate()
  //测试该属性名是否为private
  public bool isProtected()
  //测试该属性名是否为protected
  public bool isStatic()
  //测试该属性名是否为static
  public bool isDefault()
  public int getModifiers()
  //取得修饰符
  public mixed getValue(stdclass object)
  //取得该属性值
  public void setValue(stdclass object, mixed value)
  //设置该属性值
  public ReflectionClass getDeclaringClass()
  //取得定义该属性的类
  public string getDocComment()
  //取得该属性的注释
}
?>
로그인 후 복사

⑧ReflectionExtension class

<?php
class ReflectionExtension implements Reflector {
  final private __clone()
  public __construct(string name)
  public string __toString()
  public static string export()
  //导出该扩展的所有信息
  public string getName()
  //取得该扩展的名字
  public string getVersion()
  //取得该扩展的版本
  public ReflectionFunction[] getFunctions()
  //取得该扩展的所有函数
  public array getConstants()
  //取得该扩展的所有常量
  public array getINIEntries()
  //取得与该扩展相关的,在php.ini中的指令信息
  public ReflectionClass[] getClasses()
  public array getClassNames()
}

?>
로그인 후 복사

사용 예:

<?php
class Person{
 private $_name;
 
 public $age;
 
 public function __construct(){
 $this->sex = "male";
 }
 
 public function action(){
 echo "来自http://www.jb51.net的测试";
 }
}
 
$class = new ReflectionClass(&#39;Person&#39;);
//获取属性
foreach($class->getProperties() as $property) {
  echo $property->getName()."\n";
}
//获取方法
print_r($class->getMethods());
 
$p1 = new Person();
$obj = new ReflectionObject($p1);
 
//获取对象和类的属性
print_r($obj->getProperties());
로그인 후 복사

분명히 위 코드의 객체와 클래스에서 얻은 속성은 다릅니다. 이는 객체가 다음과 같기 때문입니다. 인스턴스화를 구성하므로 성별 속성을 추가하면 PHP Reflection은 실제로 많은 유용한 정보를 얻을 수 있습니다.

관련 권장사항:

새내기 PHP API 첫 경험 php api 문서 php api 인터페이스 개발 php 웹 ap

Turing Robot php API_PHP 튜토리얼 소개

나만의 PHP 프레임워크 구축 경험(2) php api 프레임워크 php 위챗 개발 프레임워크 php ci 프레임워크 강의

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

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

CakePHP 로깅 CakePHP 로깅 Sep 10, 2024 pm 05:26 PM

CakePHP에 로그인하는 것은 매우 쉬운 작업입니다. 한 가지 기능만 사용하면 됩니다. cronjob과 같은 백그라운드 프로세스에 대해 오류, 예외, 사용자 활동, 사용자가 취한 조치를 기록할 수 있습니다. CakePHP에 데이터를 기록하는 것은 쉽습니다. log() 함수는 다음과 같습니다.

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP는 오픈 소스 MVC 프레임워크입니다. 이를 통해 애플리케이션 개발, 배포 및 유지 관리가 훨씬 쉬워집니다. CakePHP에는 가장 일반적인 작업의 과부하를 줄이기 위한 여러 라이브러리가 있습니다.

See all articles