当前位置:网站首页>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
原网站

版权声明
本文为[Gu Yue's blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231030320477.html