当前位置:网站首页>PHP reflection class use
PHP reflection class use
2022-06-23 10:44:00 【Gu Yue's blog】
<?php
namespace app;
error_reporting(E_ALL);
ini_set('display_errors','on');
/**
@description Test reflection class
@author jackson.hu
@date 2022.06.15
@flag 187
*/
class MyReflection
{
public $prefix = 'GS';
public static $instance = 0;
const COMPANY = ' Guangsha ';
public static function getInstance()
{
if (self::$instance){
self::$instance = new self();
}
return self::$instance;
}
/**
@description The way to say hello
@param string name user name
@return string
@flag 187
*/
public function say($name)
{
echo '['.$this->prefix.'] '.self::COMPANY.' say hello world to you~~'.$name;
return $name;
}
}
// 1. Output class information
echo '========================== Output class information ======================================='.PHP_EOL;
\ReflectionClass::export(MyReflection::class);
$reflectionClass = new \ReflectionClass(MyReflection::class);
// 2. Get the name of the class ( With namespace )
echo PHP_EOL.'========================== Get the name of the class ( With namespace ) ======================================='.PHP_EOL;
var_dump($reflectionClass->getName());
// 3. Get class namespace ( Class name not included )
echo PHP_EOL.'========================== Get class namespace ( Class name not included ) ======================================='.PHP_EOL;
var_dump($reflectionClass->getNamespaceName());
// 4. Get class documentation
echo PHP_EOL.'========================== Get class documentation ======================================='.PHP_EOL;
var_dump($reflectionClass->getDocComment());
// 4.1 Get the value of a document property
echo PHP_EOL.'========================== Get the value of a document property ======================================='.PHP_EOL;
function getDocAttribute($doc,$attr)
{
$res = preg_match("/@{$attr}\s(.*)/m",$doc,$matches);
if ($res) {
return trim($matches[1]);
} else {
return '';
}
}
echo getDocAttribute($reflectionClass->getDocComment(),'flag').PHP_EOL;
echo getDocAttribute($reflectionClass->getDocComment(),'description').PHP_EOL;
echo getDocAttribute($reflectionClass->getDocComment(),'date').PHP_EOL;
echo getDocAttribute($reflectionClass->getDocComment(),'author').PHP_EOL;
// 5. Get class properties
// 5.1 Get class properties ( After testing, it contains common attributes and static attributes )
echo PHP_EOL.'========================== Get class properties ======================================='.PHP_EOL;
$properties = $reflectionClass->getProperties();
foreach($properties as $property){
echo $property->getName().PHP_EOL;
}
// 5.2 Get class constant properties
echo PHP_EOL.'========================== Get class constant properties ======================================='.PHP_EOL;
$const = $reflectionClass->getConstants();
var_dump($const);
foreach($const as $name => $val){
echo $name.'---------'.$val.PHP_EOL;
}
// 5.3 Get class static properties
echo PHP_EOL.'========================== Get class static properties ======================================='.PHP_EOL;
$properties = $reflectionClass->getStaticProperties();
var_dump($properties);
foreach($properties as $name => $val){
echo $name.'---------'.$val.PHP_EOL;
}
// 5.4 Get class attribute modifiers
echo PHP_EOL.'========================== Get class attribute modifiers ======================================='.PHP_EOL;
$properties = $reflectionClass->getProperties();
foreach($properties as $property){
var_dump($property->getModifiers()).PHP_EOL;
var_dump(\Reflection::getModifierNames($property->getModifiers())).PHP_EOL;
}
// 5.5 Several methods for judging access permission of class attribute
echo PHP_EOL.'========================== Get class attribute modifiers ======================================='.PHP_EOL;
$properties = $reflectionClass->getProperties();
foreach($properties as $property){
echo 'is_public:'.$property->isPublic().PHP_EOL;
echo 'is_protected:'.$property->isProtected().PHP_EOL;
echo 'is_private:'.$property->isPrivate().PHP_EOL;
echo 'is_static:'.$property->isStatic().PHP_EOL;
}
// 6. Get class method documentation
echo PHP_EOL.'========================== Get class method documentation ======================================='.PHP_EOL;
// 7. Get class method
// 7.1 Get class method ( After testing, it includes common methods and static methods )
echo PHP_EOL.'========================== Get class method ======================================='.PHP_EOL;
$methods = $reflectionClass->getMethods();
foreach($methods as $method){
echo $method->getName().PHP_EOL;
}
// 7.2 Get the method of class static method collection
// 7.3 Did not get class method parameters (php7+ edition ,8 Version passed getAttributes, No discussion for the time being )
// 7.4 Get class method modifiers
echo PHP_EOL.'========================== Get class method modifiers ======================================='.PHP_EOL;
$methods = $reflectionClass->getMethods();
foreach($methods as $method){
var_dump($method->getModifiers()).PHP_EOL;
var_dump(\Reflection::getModifierNames($method->getModifiers())).PHP_EOL;
}
// 7.5 Methods to determine access rights
echo PHP_EOL.'========================== Methods to determine access rights ======================================='.PHP_EOL;
$methods = $reflectionClass->getMethods();
foreach($methods as $method){
echo 'is_public:'.$method->isPublic().PHP_EOL;
echo 'is_protected:'.$method->isProtected().PHP_EOL;
echo 'is_private:'.$method->isPrivate().PHP_EOL;
echo 'is_final:'.$method->isFinal().PHP_EOL;
echo 'is_abstract:'.$method->isAbstract().PHP_EOL;
echo 'is_static:'.$method->isStatic().PHP_EOL;
}
// 8. Calling class methods through reflection
$method = $reflectionClass->getMethod('say');
// Hash parameters
$method->invoke(new MyReflection,'123123213213213');
// Array parameters
$method->invokeArgs(new MyReflection,['123123213213213']);
result :
========================== Output class information =======================================
Deprecated: Function ReflectionClass::export() is deprecated in D:\project\test\Reflection.php on line 44
/**
@description Test reflection class
@author jackson.hu
@date 2022.06.15
@flag 187
*/
Class [ <user> class app\MyReflection ] {
@@ D:\project\test\Reflection.php 13-40
- Constants [1] {
Constant [ public string COMPANY ] { Guangsha }
}
- Static properties [1] {
Property [ public static $instance ]
}
- Static methods [1] {
Method [ <user> static public method getInstance ] {
@@ D:\project\test\Reflection.php 19 - 27
}
}
- Properties [1] {
Property [ <default> public $prefix ]
}
- Methods [1] {
/**
@description The way to say hello
@param string name user name
@return string
@flag 187
*/
Method [ <user> public method say ] {
@@ D:\project\test\Reflection.php 35 - 39
- Parameters [1] {
Parameter #0 [ <required> $name ]
}
}
}
}
========================== Get the name of the class ( With namespace ) =======================================
string(16) "app\MyReflection"
========================== Get class namespace ( Class name not included ) =======================================
string(3) "app"
========================== Get class documentation =======================================
string(87) "/**
@description Test reflection class
@author jackson.hu
@date 2022.06.15
@flag 187
*/"
========================== Get the value of a document property =======================================
187
Test reflection class
2022.06.15
jackson.hu
========================== Get class properties =======================================
prefix
instance
========================== Get class constant properties =======================================
array(1) {
["COMPANY"]=>
string(6) " Guangsha "
}
COMPANY--------- Guangsha
========================== Get class static properties =======================================
array(1) {
["instance"]=>
int(0)
}
instance---------0
========================== Get class attribute modifiers =======================================
int(1)
array(1) {
[0]=>
string(6) "public"
}
int(17)
array(2) {
[0]=>
string(6) "public"
[1]=>
string(6) "static"
}
========================== Get class attribute modifiers =======================================
is_public:1
is_protected:
is_private:
is_static:
is_public:1
is_protected:
is_private:
is_static:1
========================== Get class method documentation =======================================
========================== Get class method =======================================
getInstance
say
========================== Get class method modifiers =======================================
int(17)
array(2) {
[0]=>
string(6) "public"
[1]=>
string(6) "static"
}
int(1)
array(1) {
[0]=>
string(6) "public"
}
========================== Methods to determine access rights =======================================
is_public:1
is_protected:
is_private:
is_final:
is_abstract:
is_static:1
is_public:1
is_protected:
is_private:
is_final:
is_abstract:
is_static:
[GS] Guangsha say hello world to you~~123123213213213[GS] Guangsha say hello world to you~~123123213213213
边栏推荐
- Explain in detail the method of judging the size end
- 六张图详解LinkedList 源码解析
- TTY drive frame
- Similarities and differences between SPI and IIC
- 5 login failures, limiting login practice
- NOI OJ 1.2 10:Hello, World!的大小 C语言
- SPI与IIC异同
- 最简单DIY基于C#和51单片机上下位机一体化的PCA9685舵机控制程序
- NOI OJ 1.2 整型与布尔型的转换 C语言
- Noi OJ 1.2 06: round floating point numbers to zero
猜你喜欢

长安LUMIN是否有能力成为微电市场的破局产品

Pycharm installation tutorial, super detailed

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

当 Pandas 遇见 SQL,一个强大的工具库诞生了

一个优秀速开发框架是什么样的?

最简单DIY串口蓝牙硬件实现方案

Why should poll/select use Nonblock when opening

Unity technical manual - lifecycle lifetimebyemitterspeed - color in the cycle coloroverlifetime- speed color colorbyspeed

ESP32-CAM无线监控智能网关的设计与实现

The problem of C language structure byte alignment
随机推荐
Mysql 的Innodb引擎和Myisam数据结构和区别
Build a QQ robot to wake up your girlfriend
php反射类使用
2021-05-11 abstract class
ESP32-CAM、ESP8266、WIFI、蓝牙、单片机、热点创建嵌入式DNS服务器
Noi OJ 1.3 15: apple and bug C language
Mathematical analysis_ Notes_ Chapter 2: real and plural numbers
New technology aesthetics and original biological networking operating system reshape the whole house intelligence
2021-05-12 internal class
JVM简单入门-01
韦东山设备信息查询例程学习
Noi OJ 1.4 05: integer size comparison C language
NOI OJ 1.4 05:整数大小比较 C语言
NOI OJ 1.4 03:奇偶数判断 C语言
MySQL-02. Understanding of indexes at work
NOI OJ 1.3 11:计算浮点数相除的余数 C语言
list的介绍及使用
实现常用C语言字符串处理函数
Stm32f1 and stm32subeide programming example - infrared tracking sensor driver
连番承压之后,苹果或将大幅提高iPhone14的售价