当前位置:网站首页>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
边栏推荐
猜你喜欢

最简单DIY基于STM32的远程控制电脑系统①(电容触摸+按键控制)

Experience of using thread pool in project

Data structures and differences between MySQL InnoDB engine and MyISAM

基于SqlSugar的开发框架循序渐进介绍(9)-- 结合Winform控件实现字段的权限控制

Analysis of LinkedList source code

最简单DIY基于51单片机的舵机控制器

Set up a QQ robot for ordering songs, and watch beautiful women

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

What is a good quick development framework like?

JVM easy start-01
随机推荐
Golang quick start (3)
Economic common sense
Noi OJ 1.3 15: apple and bug C language
Opencloudos uses snap to install NET 6
2021-05-12 interface definition and Implementation
NOI OJ 1.2 06:浮点数向零舍入
2021-05-07 constructor
Pycharm installation tutorial, super detailed
2021-04-27 classes and objects
2021-05-11 instanceof and type conversion
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
torch权重转mindspore
MySQL-02. Understanding of indexes at work
Noi OJ 1.3 14: elephant drinking water C language
Is IPv6 faster than IPv4?
Noi OJ 1.2 integer data type storage space size
社招腾讯高P(高级产品经理)的面试手册
Too helpless! Microsoft stopped selling AI emotion recognition and other technologies, saying frankly: "the law can not keep up with the development of AI"
NOI OJ 1.4 03:奇偶数判断 C语言
一个优秀速开发框架是什么样的?