生活随笔
收集整理的這篇文章主要介紹了
PHP---反射
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
所謂反射就是定義好一個類之后,通過ReflectionClass讀取類的名字從而獲取類結構信息的過程 class mycoach
{ protected $name; protected $age; public function __construct($name,$age){$ this ->name =
$name;$ this ->age =
$age;} public function eat(){echo " 嗯,吃點增加體能的東西 " .PHP_EOL;}
} $coach = new mycoach('陳培昌',22);
$a = new ReflectionClass('mycoach');
if($a->isInstantiable())
{
echo "科學的飲食和訓練".PHP_EOL;
}
$myattributes = $a->getConstructor(); var_dump($myattributes);
輸出結果:
科學的飲食和訓練
object (ReflectionMethod)#
3 (
2 ) {[ " name " ]=>
string (
11 )
" __construct " [ " class " ]=>
string (
7 )
" mycoach "
} ?
方法一??getProperties() 注意!要想實現兩個屬性的打印,務必在類結構體中聲明兩個保護屬性 (例如:protected $name) $property = $a->
getProperties();
print( ' =================property================= ' .PHP_EOL);
var_dump($property); 輸出結果:
=================property=================
array( 2 ) {[ 0 ]=>
object (ReflectionProperty)#
4 (
2 ) {[ " name " ]=>
string (
4 )
" name " [ " class " ]=>
string (
7 )
" mycoach " }[ 1 ]=>
object (ReflectionProperty)#
5 (
2 ) {[ " name " ]=>
string (
3 )
" age " [ " class " ]=>
string (
7 )
" mycoach " }
} 方法二 getMethods() getname() print(
' =================methods================= ' .PHP_EOL);
var_dump($a ->
getMethods());
print( ' =================methods by row================= ' .PHP_EOL); foreach ($a->getMethods()
as $method)
{echo $method ->
getName().PHP_EOL;
} 輸出結果:
=================methods=================
array( 2 ) {[ 0 ]=>
object (ReflectionMethod)#
6 (
2 ) {[ " name " ]=>
string (
11 )
" __construct " [ " class " ]=>
string (
7 )
" mycoach " }[ 1 ]=>
object (ReflectionMethod)#
7 (
2 ) {[ " name " ]=>
string (
3 )
" eat " [ " class " ]=>
string (
7 )
" mycoach " }
}
=================methods by row=================
__construct
eat print(
' =================execute================= ' .PHP_EOL); if ($a->hasMethod(
' eat ' ))
{$method = $a->getMethod(
' eat ' );print_r($method);
} 輸出結果:
=================execute=================
ReflectionMethod Object
([name] =>
eat[ class ] =>
mycoach
)Process finished with exit code 0 <?
php
class mycoach
{ protected $name; protected $age; public function __construct($name,$age){$ this ->name =
$name;$ this ->age =
$age;} public function eat(){echo " 嗯,吃點增加體能的東西 " .PHP_EOL;}
}
$coach =
new mycoach(
' 陳培昌 ' ,
22 );
$a =
new ReflectionClass(
' mycoach ' );
if ($a->
isInstantiable())
{echo " 科學的飲食和訓練 " .PHP_EOL;
}
$myattributes = $a->
getConstructor();
var_dump($myattributes);
$property = $a->
getProperties();
print( ' =================property================= ' .PHP_EOL);
var_dump($property);
print( ' =================methods================= ' .PHP_EOL);
var_dump($a ->
getMethods());
print( ' =================methods by row================= ' .PHP_EOL); foreach ($a->getMethods()
as $method)
{echo $method ->
getName().PHP_EOL;
}
print( ' =================execute================= ' .PHP_EOL); if ($a->hasMethod(
' eat ' ))
{$method = $a->getMethod(
' eat ' );print_r($method);
} ?
轉載于:https://www.cnblogs.com/saintdingspage/p/11588609.html
總結
以上是生活随笔 為你收集整理的PHP---反射 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。