麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

php反射類的使用及Laravel對反射的使用介紹

2020-03-22 20:16:14
字體:
來源:轉載
供稿:網友
本篇文章給大家?guī)淼膬热菔顷P于php反射類的使用及Laravel對反射的使用介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

前言

PHP的反射類與實例化對象作用相反,實例化是調用封裝類中的方法、成員,而反射類則是拆封類中的所有方法、成員變量,并包括私有方法等。就如“解刨”一樣,我們可以調用任何關鍵字修飾的方法、成員。當然在正常業(yè)務中是建議不使用,比較反射類已經摒棄了封裝的概念。

本章講解反射類的使用及Laravel對反射的使用。

反射

反射類是PHP內部類,無需加載即可使用,你可以通過實例化 ReflectionClass 類去使用它。

方法

這里列舉下PHP反射類常用的方法

方法名注釋ReflectionClass::getConstant獲取定義過的一個常量ReflectionClass::getConstants獲取一組常量ReflectionClass::getConstructor獲取類的html' target='_blank'>構造函數ReflectionClass::getDefaultProperties獲取默認屬性ReflectionClass::getDocComment獲取文檔注釋ReflectionClass::getEndLine獲取最后一行的行數ReflectionClass::getFileName獲取定義類的文件名ReflectionClass::getInterfaceNames獲取接口(interface)名稱ReflectionClass::getMethods獲取方法的數組ReflectionClass::getModifiers獲取類的修飾符ReflectionClass::getName獲取類名ReflectionClass::getNamespaceName獲取命名空間的名稱ReflectionClass::getParentClass獲取父類

等等等等.... 所有關于類的方法、屬性及其繼承的父類、實現的接口都可以查詢到。
詳細文檔請參考網址

栗子
 ?php namespace A/B; class Foo { } $function = new /ReflectionClass( stdClass  var_dump($function- inNamespace()); var_dump($function- getName()); var_dump($function- getNamespaceName()); var_dump($function- getShortName()); $function = new /ReflectionClass( A//B//Foo  var_dump($function- inNamespace()); var_dump($function- getName()); var_dump($function- getNamespaceName()); var_dump($function- getShortName());? 

輸出結果

bool(false)string(8) stdClass string(0) string(8) stdClass bool(true)string(7) A/B/Foo string(3) A/B string(3) Foo 
Laravel

Laravel在實現服務容器加載時使用了反射類。現在我們開啟“解刨”模式

入口文件index.php
$app = require_once __DIR__. /../bootstrap/app.php |--------------------------------------------------------------------------| Run The Application|--------------------------------------------------------------------------| Once we have the application, we can handle the incoming request| through the kernel, and send the associated response back to| the client s browser allowing them to enjoy the creative| and wonderful application we have prepared for them.$kernel = $app- make(Illuminate/Contracts/Http/Kernel::class);$response = $kernel- handle( $request = Illuminate/Http/Request::capture()$response- send();$kernel- terminate($request, $response);

是引用語句發(fā)生的下一行調用了make方法。各位很清楚,make方法用于解析類,所有make方法的實現一定是在引用的文件內。

bootstrap/app.php
$app = new Illuminate/Foundation/Application( realpath(__DIR__. /../ ));

laravel開始加載它的核心類,所有的實現從 Illuminate/Foundation/Application 開始。

Illuminate/Foundation/Application
public function make($abstract, array $parameters = []) $abstract = $this- getAlias($abstract); if (isset($this- deferredServices[$abstract]) ! isset($this- instances[$abstract])) { $this- loadDeferredProvider($abstract); return parent::make($abstract, $parameters);}

在核心類中你可能準確的查找到make方法的存在,它加載了服務提供者隨后調用了父類的方法make,要知道作為獨立的模塊 “服務容器”是絕對不能寫在核心類的。懂點設計模式的都很清楚。

Illuminate/Container/Container

以 $api = $this- app- make( HelpSpot/API ,[ id = 為例來講解

// 真正的make方法,它直接調用了resolve繼續(xù)去實現make的功能// $abstract = HelpSpot/API public function make($abstract, array $parameters = []) // $abstract = HelpSpot/API  return $this- resolve($abstract, $parameters);protected function resolve($abstract, $parameters = []) // 判斷是否可以合理反射 // $abstract = HelpSpot/API  if ($this- isBuildable($concrete, $abstract)) { // 實例化具體實例 (實際并不是實例化,而是通過反射“解刨”了) $object = $this- build($concrete); } else { $object = $this- make($concrete);public function build($concrete) // $concrete = HelpSpot/API  if ($concrete instanceof Closure) { return $concrete($this, $this- getLastParameterOverride()); // 實例化反射類 $reflector = new ReflectionClass($concrete); // 檢查類是否可實例化 if (! $reflector- isInstantiable()) { return $this- notInstantiable($concrete); $this- buildStack[] = $concrete; // 獲取類的構造函數 $constructor = $reflector- getConstructor(); if (is_null($constructor)) { array_pop($this- buildStack); return new $concrete; $dependencies = $constructor- getParameters(); $instances = $this- resolveDependencies( $dependencies array_pop($this- buildStack); // 從給出的參數創(chuàng)建一個新的類實例。 return $reflector- newInstanceArgs($instances);}

可見一個服務容器就加載成功了。

以上就是php反射類的使用及Laravel對反射的使用介紹的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑人一级片视频 | 国产精品久久国产精麻豆96堂 | 亚洲国产美女视频 | 成人性视频欧美一区二区三区 | 成人福利视频在 | 色女人在线| 国产精品99久久久久久宅女 | 伊人一二三四区 | 成人精品 | 国产精品免费成人 | 啪啪激情 | 欧美乱论 | 亚洲网站在线观看 | 欧美一a一片一级一片 | av电影免费播放 | 亚洲第一成人久久网站 | 日本欧美一区二区三区在线播 | 亚洲欧美不卡视频 | 亚洲精品日韩色噜噜久久五月 | 黄色大片网站在线观看 | 欧美成人精品一区 | 国产成人观看 | 中文字幕综合在线观看 | 国产91丝袜在线播放 | 欧美18一12sex性处hd | 免费播放av | 午夜小网站 | 久国产| 国产精品久久久久久久久久久天堂 | 99精品视频在线观看免费 | av电影免费在线 | 久久免费毛片 | 久久久电影电视剧免费看 | 成年人免费高清视频 | 91av在线免费| 国产免费人做人爱午夜视频 | 国产精品美女久久久免费 | 日韩区在线 | 羞羞答答xxdd在线播放 | 日韩欧美高清一区 | 激情影院在线观看 |