백엔드 개발 PHP 튜토리얼 ReflectionClass:PHP中的反照类

ReflectionClass:PHP中的反照类

Jun 13, 2016 pm 01:01 PM
function nbsp plugin

ReflectionClass:PHP中的反射类
interface Iplugin{
public static function getName();
}
function findPlugins(){
$plugins = array();
foreach (get_declared_classes() as $class){
$reflectionClass = new ReflectionClass($class);
if ($reflectionClass->implementsInterface('Iplugin')) {
$plugins[] = $reflectionClass;
}
}
return $plugins;
}
function computeMenu(){
$menu = array();
foreach (findPlugins() as $plugin){
if ($plugin->hasMethod('getMenuItems')) {
$reflectionMethod = $plugin->getMethod('getMenuItems');
if ($reflectionMethod->isStatic()) {
$items = $reflectionMethod->invoke(null);
} else {
$pluginInstance = $plugin->newInstance();
$items = $reflectionMethod->invoke($pluginInstance);
}
$menu = array_merge($menu,$items);
}
}
return $menu;
}
function computeArticles(){
$articles = array();
foreach (findPlugins() as $plugin){
if ($plugin->hasMethod('getArticles')) {
$reflectionMethod = $plugin->getMethod('getArticles');
if ($reflectionMethod->isStatic()) {
$items = $reflectionMethod->invoke(null);
} else {
$pluginInstance = $plugin->newInstance();
$items = $reflectionMethod->invoke($pluginInstance);
}
$articles = array_merge($articles,$items);
}
}
return $articles;
}
require_once('plugin.php');
$menu = computeMenu();
$articles = computeArticles();
print_r($menu);
print_r($articles);
//plugin.php 代码如下
class MycoolPugin implements Iplugin {
public static function getName(){
return 'MycoolPlugin';
}
public static function getMenuItems(){
return array(array('description'=>'MycoolPlugin','link'=>'/MyCoolPlugin'));
}
public static function getArticles(){
return array(array('path'=>'/MycoolPlugin','title'=>'This is a really cool article','text'=>xxxxxxxxx));
}
}上述代码是php反射类的一个应用。
什么是php反射类,顾名思义,可以理解为一个类的映射。
举个例子:

class fuc {        //定义一个类
static function ec() {
echo '我是一个类';
}
}
$class=new ReflectionClass('fuc');    //建立 fuc这个类的反射类
至于$class 这反射类里有什么东东,可以查看手册,这里不详解了

$fuc=$class->newInstance();  //相当于实例化 fuc 类
$fuc->ec(); //执行 fuc 里的方法ec

其中还有一些更高级的用法

$ec=$class->getmethod('ec');  //获取fuc 类中的ec方法
$fuc=$class->newInstance();  //实例化
$ec->invoke($fuc);      //执行ec 方法
上面的过程很熟悉吧。其实和调用对象的方法类似
只不过这里是反着来的,方法在前,对象在后

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

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

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

SublimeText3 중국어 버전

SublimeText3 중국어 버전

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

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

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

해결 방법: 조직에서 PIN 변경을 요구합니다. 해결 방법: 조직에서 PIN 변경을 요구합니다. Oct 04, 2023 pm 05:45 PM

해결 방법: 조직에서 PIN 변경을 요구합니다.

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Sep 22, 2023 am 11:37 AM

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Sep 14, 2023 pm 03:33 PM

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까?

Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Jul 16, 2023 pm 03:29 PM

Windows 11/10 복구의 OOBELANGUAGE 오류 문제

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Sep 15, 2023 pm 03:57 PM

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법

Windows 11의 디스플레이 크기 조정 가이드 Windows 11의 디스플레이 크기 조정 가이드 Sep 19, 2023 pm 06:45 PM

Windows 11의 디스플레이 크기 조정 가이드

Windows 11에서 밝기를 조정하는 10가지 방법 Windows 11에서 밝기를 조정하는 10가지 방법 Dec 18, 2023 pm 02:21 PM

Windows 11에서 밝기를 조정하는 10가지 방법

Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까? Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까? Nov 29, 2023 pm 11:21 PM

Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까?

See all articles