직접 작성한 PHP API 프레임워크 반영 소개(3)
이전 글 "Handwriting PHP API Framework - Composer 설치 및 사용(2)"에서는 Composer의 설치 및 사용에 대해 소개했습니다. 이번 글에서는 Reflection의 개념을 소개하겠습니다.
반성, 직관적 이해는 도착지를 기준으로 출발지와 출처를 찾는 것입니다. 리플렉션은 PHP 실행 상태에서 PHP 프로그램 분석을 확장하고, 주석을 포함하여 클래스, 메서드, 속성, 매개 변수 등에 대한 자세한 정보를 내보내거나 제안하는 것을 의미합니다. 동적으로 정보를 얻고 객체 메소드를 동적으로 호출하는 기능을 리플렉션 API라고 합니다.
먼저 데모를 살펴보겠습니다:
<?php function p($msg, $var) { echo($msg.":".print_r($var, true)).PHP_EOL.PHP_EOL; } class Demo { private $id; protected $name; public $skills = []; public function __construct($id, $name, $skills = []) { $this->id = $id; $this->name = $name; $this->skills = $skills; } public function getName() { return $this->name; } public function getSkill() { p('skill', $this->skills); } } $ref = new ReflectionClass('Demo'); if ($ref->isInstantiable()) { p('检查类是否可实例化isInstantiable', null); } $constructor = $ref->getConstructor(); p('获取构造函数getConstructor', $constructor); $parameters = $constructor->getParameters(); foreach ($parameters as $param) { p('获取参数getParameters', $param); } if ($ref->hasProperty('name')) { $attr = $ref->getProperty('name'); p('获取属性getProperty', $attr); } $attributes = $ref->getProperties(); foreach ($attributes as $row) { p('获取属性列表getProperties', $row->getName()); } if ($ref->hasMethod('getSkill')) { $method = $ref->getMethod('getSkill'); p('获取方法getMethod', $method); } $methods = $ref->getMethods(); foreach ($methods as $row) { p('获取方法列表getMethods', $row->getName()); } $instance = $ref->newInstanceArgs([1, 'sai', ['php', 'js']]); p('newInstanceArgs', $instance);
출력:
➜ php git:(main) php reflect.php 检查类是否可实例化isInstantiable: 获取构造函数getConstructor:ReflectionMethod Object ( [name] => __construct [class] => Demo ) 获取参数getParameters:ReflectionParameter Object ( [name] => id ) 获取参数getParameters:ReflectionParameter Object ( [name] => name ) 获取参数getParameters:ReflectionParameter Object ( [name] => skills ) 获取属性getProperty:ReflectionProperty Object ( [name] => name [class] => Demo ) 获取属性列表getProperties:id 获取属性列表getProperties:name 获取属性列表getProperties:skills 获取方法getMethod:ReflectionMethod Object ( [name] => getSkill [class] => Demo ) 获取方法列表getMethods:__construct 获取方法列表getMethods:getName 获取方法列表getMethods:getSkill newInstanceArgs:Demo Object ( [id:Demo:private] => 1 [name:protected] => sai [skills] => Array ( [0] => php [1] => js ) )
ReflectionClass 클래스는 데모에서 사용됩니다. 물론 ReflectionClass 클래스는 이러한 메서드로 제한되지 않습니다.
더 많은 메소드
ReflectionClass 클래스에는 더 많은 메소드가 있습니다:
Method | Description |
---|---|
ReflectionClass::__construct | ReflectionClass 클래스 초기화 |
ReflectionClass::export | 클래스 내보내기 |
ReflectionClass::getConstant | 정의된 항목 가져오기 상수 |
ReflectionClass::getConstants | 상수 집합 가져오기 |
ReflectionClass::getConstructor | 클래스 생성자 가져오기 |
ReflectionClass::getDefaultProperties | 기본 속성 가져오기 |
ReflectionClass: ; ReflectionClass: ; ionClass ::getInterfaces | 인터페이스 가져오기 |
ReflectionClass::getMethod | 클래스 메소드의 ReflectionMethod를 가져옵니다. |
ReflectionClass::getMethods | 메서드 배열 가져오기 |
ReflectionClass::getModifiers | 클래스 수정자 가져오기 |
ReflectionClass::getName | 클래스 이름 가져오기 |
ReflectionClass ::getNamespaceName | 네임스페이스 이름 가져오기 |
ReflectionClass::getParentClass | 상위 클래스 가져오기 |
ReflectionClass::getProperties | 속성 집합 가져오기 |
ReflectionClass::getProperty | 클래스의 속성 가져오기 ReflectionProperty |
ReflectionClass::getReflectionConstant | 클래스 상수에 대한 ReflectionClassConstant를 가져옵니다. |
ReflectionClass::getReflectionConstants | 클래스 상수를 가져옵니다 |
tName | 짧은 이름을 받으세요 |
ReflectionClass:: getTraitAliases | 특성 별칭 배열을 반환합니다. |
ReflectionClass::getTraitNames | 이 클래스에서 사용하는 특성 이름 배열을 반환합니다. |
ReflectionClass::getTraits | 배열을 반환합니다. |
ReflectionClass::hasConstant | 상수가 정의되었는지 확인하세요 |
ReflectionClass::hasMethod | 메서드가 정의되었는지 확인하세요 |
ReflectionClass::hasProperty | 속성이 정의되었는지 확인하세요 |
ReflectionClass::implementsInterface | 인터페이스 구현 |
ReflectionClass::inNamespace | 네임스페이스에 있는지 확인 |
ReflectionClass::isAbstract | 여부를 확인 클래스는 추상 클래스입니다(추상) |
ReflectionClass::is Anonymous | 클래스가 익명 클래스인지 확인하세요 |
ReflectionClass::isCloneable | 클래스가 복사 가능한지 여부를 반환 |
ReflectionClass:: isFinal | 클래스가 final로 선언되었는지 확인 |
ReflectionClass::isInstance | 클래스 인스턴스 확인 |
ReflectionClass::isInstantiable | 클래스가 인스턴스화 가능한지 확인 |
ReflectionClass::is 인터페이스 | 클래스가 인터페이스인지 확인하세요 |
ReflectionClass::isInternal | 클래스를 확장 또는 코어로 내부적으로 정의했는지 확인하세요 |
ReflectionClass::isIterable | 이 클래스가 반복 가능한지 확인 |
ReflectionClass:: isIterateable | 반복 가능 여부 확인(반복 가능) |
ReflectionClass::isSubclassOf | 하위 클래스인지 확인 |
ReflectionClass::isTrait | 특성인지 여부를 반환합니다 |
ReflectionClass::isUserDefined | 사용자가 정의했는지 확인하세요 |
ReflectionClass::newInstance | From 지정된 인수로 새 클래스 인스턴스가 생성됩니다. |
ReflectionClass::newInstanceArgs | 주어진 인수로 새 클래스 인스턴스가 생성됩니다. |
ReflectionClass::newInstanceWithoutConstructor | 생성자를 호출하지 않고 새 클래스 인스턴스를 만듭니다. |
ReflectionClass::setStaticPropertyValue | 정적 속성 값 설정 |
ReflectionClass::__toString | Return ReflectionClass 문자열 객체의 표현. |
강력한 ReflectionClass 외에도 Reflection, ReflectionClassConstant, ReflectionMethod, ReflectionFunctionAbstract 등도 있습니다. 매뉴얼을 확인하는 것이 좋습니다: Practical Application of Reflection
이 섹션은 상대적으로 독립적이므로 다음 장에서 사용할 것입니다. 추천 학습: " PHP 비디오 튜토리얼 |
위 내용은 직접 작성한 PHP API 프레임워크 반영 소개(3)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

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

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

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

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

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

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

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

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

뜨거운 주제











이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

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

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

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