ReflectionClass与Closure
生活随笔
收集整理的這篇文章主要介紹了
ReflectionClass与Closure
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php
/*** Class A*/
class A{}$obj = new ReflectionClass('A');
var_export($obj.PHP_EOL);
類后面加上PHP_EOL會把當前類的詳細接口文檔打印出來。
ReflectionClass 可以利用這個動態創建類,動態使用類方法參數。
try{ //如果存在控制器名字的類 if(class_exists($this->getController())) { //利用反射api構造一個控制器類對應的反射類 $rc = new ReflectionClass($this->getController()); //如果該類實現 了IController接口 if($rc->implementsInterface('IController')) { //該類擁有解析后的action字符串所指向的方法名 if($rc->hasMethod($this->getAction())) { //構造一個控制器類的實例 $controller = $rc->newInstance(); //獲取該類$action參數所指向的方法對象 $method = $rc->getMethod($this->getAction()); //反射類方法對象的調用方式: $method->invoke($controller); } else { //以下為可能拋出異常 throw new Exception("Action"); } } else { throw new Exception("Interface"); } } else { throw new Exception("Controller"); }}catch(exception $e){echo $e;}Closure //閉包的意思。這個跟javascript的閉包有點類似。
function getClosure(){ return function(){}; } var_dump(getClosure());//可以看到是一個 Closure Object轉載于:https://www.cnblogs.com/canbefree/p/5212531.html
總結
以上是生活随笔為你收集整理的ReflectionClass与Closure的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 气象数据下载网站整理
- 下一篇: 移动端meta标签