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。

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

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

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

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Penyesuai Pelayan SAP NetWeaver untuk Eclipse
Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

Dreamweaver Mac版
Alat pembangunan web visual

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

VSCode Windows 64-bit Muat Turun
Editor IDE percuma dan berkuasa yang dilancarkan oleh Microsoft
