日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php 反射原理,PHP反射机制详解

發布時間:2024/2/28 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 反射原理,PHP反射机制详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要和大家分享PHP反射機制詳解,內容包括1.自動生成文檔2.實現 MVC 架構3.實現單元測試4.配合 DI 容器解決依賴,希望能幫助到大家。

1.自動生成文檔

根據反射的分析類,接口,函數和方法的內部結構,方法和函數的參數,以及類的屬性和方法,可以自動生成文檔。<?phpclass Student{

const NORMAL = 1; const FORBIDDEN = 2; /**

* 用戶ID

* @var 類型

*/

public $id; /**

* 獲取id

* @return int

*/

public function getId()

{

return $this->id;

} public function setId($id = 1)

{

$this->id = $id;

}

}$ref = new ReflectionClass('Student');$doc = $ref->getDocComment();echo $ref->getName() . ':' . getComment($ref) , "
";echo "屬性列表:
";

printf("%-15s%-10s%-40s
", 'Name', 'Access', 'Comment');$attr = $ref->getProperties();foreach ($attr as $row) {

printf("%-15s%-10s%-40s
", $row->getName(), getAccess($row), getComment($row));

}echo "常量列表:
";

printf("%-15s%-10s
", 'Name', 'Value');$const = $ref->getConstants();foreach ($const as $key => $val) {

printf("%-15s%-10s
", $key, $val);

}echo "
";echo "方法列表
";

printf("%-15s%-10s%-30s%-40s
", 'Name', 'Access', 'Params', 'Comment');$methods = $ref->getMethods();foreach ($methods as $row) {

printf("%-15s%-10s%-30s%-40s
", $row->getName(), getAccess($row), getParams($row), getComment($row));

}// 獲取權限function getAccess($method){

if ($method->isPublic()) { return 'Public';

} if ($method->isProtected()) { return 'Protected';

} if ($method->isPrivate()) { return 'Private';

}

}// 獲取方法參數信息function getParams($method){

$str = ''; $parameters = $method->getParameters(); foreach ($parameters as $row) { $str .= $row->getName() . ','; if ($row->isDefaultValueAvailable()) { $str .= "Default: {$row->getDefaultValue()}";

}

} return $str ? $str : '';

}// 獲取注釋function getComment($var){

$comment = $var->getDocComment(); // 簡單的獲取了第一行的信息,這里可以自行擴展

preg_match('/\* (.*) *?/', $comment, $res); return isset($res[1]) ? $res[1] : '';

}

輸出結果:Student:

屬性列表:

Name Access Comment

id Public 用戶ID

常量列表:

Name Value

NORMAL 1 FORBIDDEN 2 方法列表

Name Access Params Comment

getId Public 獲取id

setId Public id,Default: 1

2.實現 MVC 架構

現在好多框架都是 MVC 的架構,根據路由信息定位控制器($controller) 和方法($method) 的名稱,之后使用反射實現自動調用。$class = new ReflectionClass(ucfirst($controller) . 'Controller');$controller = $class->newInstance();if ($class->hasMethod($method)) { $method = $class->getMethod($method); $method->invokeArgs($controller, $arguments);

} else {

throw new Exception("{$controller} controller method {$method} not exists!");

}

3.實現單元測試

一般情況下我們會對函數和類進行測試,判斷其是否能夠按我們預期返回結果,我們可以用反射實現一個簡單通用的類測試用例。<?phpclass Calc{

public function plus($a, $b)

{

return $a + $b;

} public function minus($a, $b)

{

return $a - $b;

}

}function testEqual($method, $assert, $data){

$arr = explode('@', $method); $class = $arr[0]; $method = $arr[1]; $ref = new ReflectionClass($class); if ($ref->hasMethod($method)) { $method = $ref->getMethod($method); $res = $method->invokeArgs(new $class, $data); if($res === $assert){ echo "測試結果正確";

};

}

}

testEqual('Calc@plus', 3, [1, 2]);echo "
";

testEqual('Calc@minus', -1, [1, 2]);

這是類的測試方法,也可以利用反射實現函數的測試方法。<?phpfunction title($title, $name){

return sprintf("%s. %s\r\n", $title, $name);

}$function = new ReflectionFunction('title');echo $function->invokeArgs(array('Dr', 'Phil'));?>

這里只是我簡單寫的一個測試用例,PHPUnit 單元測試框架很大程度上依賴了 Reflection 的特性,可以了解下。

4.配合 DI 容器解決依賴

Laravel 等許多框架都是使用 Reflection 解決依賴注入問題,具體可查看 Laravel 源碼進行分析。

下面我們代碼簡單實現一個 DI 容器演示 Reflection 解決依賴注入問題。<?phpclass DI{

protected static $data = []; public function __set($k, $v)

{

self::$data[$k] = $v;

} public function __get($k)

{

return $this->bulid(self::$data[$k]);

} // 獲取實例

public function bulid($className)

{

// 如果是匿名函數,直接執行,并返回結果

if ($className instanceof Closure) { return $className($this);

} // 已經是實例化對象的話,直接返回

if(is_object($className)) { return $className;

} // 如果是類的話,使用反射加載

$ref = new ReflectionClass($className); // 監測類是否可實例化

if (!$ref->isInstantiable()) { throw new Exception('class' . $className . ' not find');

} // 獲取構造函數

$construtor = $ref->getConstructor(); // 無構造函數,直接實例化返回

if (is_null($construtor)) { return new $className;

} // 獲取構造函數參數

$params = $construtor->getParameters(); // 解析構造函數

$dependencies = $this->getDependecies($params); // 創建新實例

return $ref->newInstanceArgs($dependencies);

} // 分析參數,如果參數中出現依賴類,遞歸實例化

public function getDependecies($params)

{

$data = []; foreach($params as $param)

{ $tmp = $param->getClass(); if (is_null($tmp)) { $data[] = $this->setDefault($param);

} else { $data[] = $this->bulid($tmp->name);

}

} return $data;

} // 設置默認值

public function setDefault($param)

{

if ($param->isDefaultValueAvailable()) { return $param->getDefaultValue();

} throw new Exception('no default value!');

}

}class Demo{

public function __construct(Calc $calc)

{

echo $calc->plus(1, 2);

}

}class Calc{

public function plus($a, $b)

{

return $a + $b;

} public function minus($a, $b)

{

return $a - $b;

}

}$di = new DI();$di->calc = 'Calc';

$di->demo = 'Demo';$di->demo;//輸出結果為3

相關推薦:

總結

以上是生活随笔為你收集整理的php 反射原理,PHP反射机制详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。