PHP 反射(Reflection)使用实例
这篇文章主要介绍了PHP 反射(Reflection)使用实例,本文讲解了ReflectionClass、ReflectionExtension、 ReflectionFunction、ReflectionMethod、ReflectionObject、ReflectionParameter等类的使用实例,需要的朋友可以参考下
PHP Reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。
ReflectionClass类获取类相关信息,如获取属性、方法、文档注释等。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
class Person { /** * For the sake of demonstration, we"re setting this private */ private $_allowDynamicAttributes = false;
/** type=primary_autoincrement */ protected $id = 0;
/** type=varchar length=255 null */ protected $name;
/** type=text null */ protected $biography;
public function getId() { return $this->id; } public function setId($v) { $this->id = $v; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } public function getBiography() { return $this->biography; } public function setBiography($v) { $this->biography = $v; } }
//导出类 ReflectionClass::export('Person');
$r = new ReflectionClass('Person');
//获取所有属性 print_r($r->getProperties());
/** * 获取指定属性 * ReflectionProperty::IS_STATIC * ReflectionProperty::IS_PUBLIC * ReflectionProperty::IS_PROTECTED * ReflectionProperty::IS_PRIVATE */ print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
//获取注释 print_r($r->getProperty('id')->getDocComment());
//获取方法 print_r($r->getMethods()); |
ReflectionExtension 类用于获取扩展相关信息
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$re = new ReflectionExtension('Reflection'); print_r($re->getClasses()); //扩展的所有类 print_r($re->getClassNames()); //扩展所有类名
$dom = new ReflectionExtension('mysql'); print_r($dom->getConstants());//扩展常量 print_r($dom->getDependencies());//该扩展依赖 print_r($dom->getFunctions());//扩展方法 print_r($dom->getINIEntries());//扩展ini信息 print_r($dom->getName());//扩展名称 print_r($dom->getVersion());//扩展版本 print_r($dom->info());//扩展信息 print_r($dom->isPersistent());//是否是持久扩展 print_r($dom->isTemporary()); //是否是临时扩展 |
ReflectionFunction类 用户获取函数相关信息
?
1 2 3 4 5 |
$rf = new ReflectionFunction('array_merge');
foreach($rf->getParameters() as $item) { echo $item . PHP_EOL; } |
ReflectionMethod类用户获取方法相关信息
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Person {
public $name;
/** * get name of person */ public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } }
$rm = new ReflectionMethod('Person', 'getName');
print_r($rm->isPublic()); print_r($rm->getDocComment()); |
ReflectionObject 类 用于获取对象相关信息
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Person {
public $name;
public function __construct($name) { $this->name = $name; }
public function getName() { return $this->name; }
public function setName($v) { $this->name = $v; } }
$a = new Person('a');
$ro = new ReflectionObject($a);
print_r($ro->getMethods()); |
ReflectionParameter 获取函数或方法参数的相关信息。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Person {
public $name;
public function __construct($name) { $this->name = $name; }
public function getName() { return $this->name; }
public function setName($v) { $this->name = $v; } }
$p = new ReflectionParameter(array('Person', 'setName'), 0);
print_r($p->getPosition()); //0 print_r($p->getName()); //v |
ReflectionProperty 获取类的属性的相关信息。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Person {
/** 测试 */ public $name;
public function __construct($name) { $this->name = $name; }
public function getName() { return $this->name; }
public function setName($v) { $this->name = $v; } }
$p = new ReflectionProperty('Person', 'name');
print_r($p->getDocComment()); |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

SublimeText3 Linux neue Version
SublimeText3 Linux neueste Version

SecLists
SecLists ist der ultimative Begleiter für Sicherheitstester. Dabei handelt es sich um eine Sammlung verschiedener Arten von Listen, die häufig bei Sicherheitsbewertungen verwendet werden, an einem Ort. SecLists trägt dazu bei, Sicherheitstests effizienter und produktiver zu gestalten, indem es bequem alle Listen bereitstellt, die ein Sicherheitstester benötigen könnte. Zu den Listentypen gehören Benutzernamen, Passwörter, URLs, Fuzzing-Payloads, Muster für vertrauliche Daten, Web-Shells und mehr. Der Tester kann dieses Repository einfach auf einen neuen Testcomputer übertragen und hat dann Zugriff auf alle Arten von Listen, die er benötigt.

WebStorm-Mac-Version
Nützliche JavaScript-Entwicklungstools

SublimeText3 Englische Version
Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!
