濮阳杆衣贸易有限公司

主頁 > 知識庫 > Laravel框架源碼解析之反射的使用詳解

Laravel框架源碼解析之反射的使用詳解

熱門標簽:辦理400電話哪家好點 邢臺400電話辦理 重慶外呼電銷系統多少錢 南京3D地圖標注 南寧電話外呼系統線路 嘟嘟云外呼系統 正規(guī)電銷機器人系統 濟源百應電銷機器人聯系方式 咸陽電銷

本文實例講述了Laravel框架源碼解析之反射的使用。分享給大家供大家參考,具體如下:

前言

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

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

反射

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

方法

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

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

等等等等.... 所有關于類的方法、屬性及其繼承的父類、實現的接口都可以查詢到。
詳細文檔請參考官網: http://php.net/manual/zh/class.reflectionclass.php

栗子

?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'=>1]); 為例來講解

// 真正的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);
}

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

更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • Laravel框架源碼解析之模型Model原理與用法解析
  • Laravel框架源碼解析之入口文件原理分析
  • Laravel 框架控制器 Controller原理與用法實例分析
  • Laravel框架數據庫CURD操作、連貫操作總結
  • PHP開發(fā)框架Laravel數據庫操作方法總結
  • Laravel框架中擴展函數、擴展自定義類的方法
  • Laravel框架路由配置總結、設置技巧大全
  • Laravel 5 框架入門(一)
  • Laravel 5框架學習之數據庫遷移(Migrations)
  • Laravel 5框架學習之向視圖傳送數據
  • Laravel 5框架學習之用戶認證
  • Laravel框架集合用法實例淺析

標簽:平頂山 黃山 隴南 河南 唐山 武漢 通遼 南通

巨人網絡通訊聲明:本文標題《Laravel框架源碼解析之反射的使用詳解》,本文關鍵詞  Laravel,框架,源碼,解析,之,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel框架源碼解析之反射的使用詳解》相關的同類信息!
  • 本頁收集關于Laravel框架源碼解析之反射的使用詳解的相關信息資訊供網民參考!
  • 推薦文章
    龙门县| 垦利县| 正蓝旗| 石狮市| 贵阳市| 安西县| 渭源县| 昌吉市| 尚义县| 兴海县| 桐梓县| 西平县| 漳平市| 溧水县| 马公市| 蕉岭县| 尉犁县| 得荣县| 大庆市| 济宁市| 哈尔滨市| 周至县| 温泉县| 方山县| 文水县| 休宁县| 黑河市| 塘沽区| 巩义市| 汕头市| 信丰县| 穆棱市| 罗江县| 时尚| 永德县| 溆浦县| 崇仁县| 甘德县| 郓城县| 岳普湖县| 灵寿县|