Laravel框架源碼解析之反射的使用詳解
本文實(shí)例講述了Laravel框架源碼解析之反射的使用。分享給大家供大家參考,具體如下:
前言
PHP的反射類(lèi)與實(shí)例化對(duì)象作用相反,實(shí)例化是調(diào)用封裝類(lèi)中的方法、成員,而反射類(lèi)則是拆封類(lèi)中的所有方法、成員變量,并包括私有方法等。就如“解刨”一樣,我們可以調(diào)用任何關(guān)鍵字修飾的方法、成員。當(dāng)然在正常業(yè)務(wù)中是建議不使用,比較反射類(lèi)已經(jīng)摒棄了封裝的概念。
本章講解反射類(lèi)的使用及Laravel對(duì)反射的使用。
反射
反射類(lèi)是PHP內(nèi)部類(lèi),無(wú)需加載即可使用,你可以通過(guò)實(shí)例化 ReflectionClass 類(lèi)去使用它。
方法
這里列舉下PHP反射類(lèi)常用的方法
| 方法名 | 注釋 |
|---|---|
| ReflectionClass::getConstant | 獲取定義過(guò)的一個(gè)常量 |
| ReflectionClass::getConstants | 獲取一組常量 |
| ReflectionClass::getConstructor | 獲取類(lèi)的構(gòu)造函數(shù) |
| ReflectionClass::getDefaultProperties | 獲取默認(rèn)屬性 |
| ReflectionClass::getDocComment | 獲取文檔注釋 |
| ReflectionClass::getEndLine | 獲取最后一行的行數(shù) |
| ReflectionClass::getFileName | 獲取定義類(lèi)的文件名 |
| ReflectionClass::getInterfaceNames | 獲取接口(interface)名稱(chēng) |
| ReflectionClass::getMethods | 獲取方法的數(shù)組 |
| ReflectionClass::getModifiers | 獲取類(lèi)的修飾符 |
| ReflectionClass::getName | 獲取類(lèi)名 |
| ReflectionClass::getNamespaceName | 獲取命名空間的名稱(chēng) |
| ReflectionClass::getParentClass | 獲取父類(lèi) |
等等等等.... 所有關(guān)于類(lèi)的方法、屬性及其繼承的父類(lèi)、實(shí)現(xiàn)的接口都可以查詢(xún)到。
詳細(xì)文檔請(qǐng)參考官網(wǎng): 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());
?>
輸出結(jié)果
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在實(shí)現(xiàn)服務(wù)容器加載時(shí)使用了反射類(lèi)。現(xiàn)在我們開(kāi)啟“解刨”模式
入口文件
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);
是引用語(yǔ)句發(fā)生的下一行調(diào)用了make方法。各位很清楚,make方法用于解析類(lèi),所有make方法的實(shí)現(xiàn)一定是在引用的文件內(nèi)。
bootstrap\app.php
$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/../') );
laravel開(kāi)始加載它的核心類(lèi),所有的實(shí)現(xiàn)從 Illuminate\Foundation\Application 開(kāi)始。
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);
}
在核心類(lèi)中你可能準(zhǔn)確的查找到make方法的存在,它加載了服務(wù)提供者隨后調(diào)用了父類(lèi)的方法make,要知道作為獨(dú)立的模塊 “服務(wù)容器”是絕對(duì)不能寫(xiě)在核心類(lèi)的。懂點(diǎn)設(shè)計(jì)模式的都很清楚。
Illuminate\Container\Container
以 $api = $this->app->make('HelpSpot\API',['id'=>1]); 為例來(lái)講解
// 真正的make方法,它直接調(diào)用了resolve繼續(xù)去實(shí)現(xiàn)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)) {
// 實(shí)例化具體實(shí)例 (實(shí)際并不是實(shí)例化,而是通過(guò)反射“解刨”了)
$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());
}
// 實(shí)例化反射類(lèi)
$reflector = new ReflectionClass($concrete);
// 檢查類(lèi)是否可實(shí)例化
if (! $reflector->isInstantiable()) {
return $this->notInstantiable($concrete);
}
$this->buildStack[] = $concrete;
// 獲取類(lèi)的構(gòu)造函數(shù)
$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);
// 從給出的參數(shù)創(chuàng)建一個(gè)新的類(lèi)實(shí)例。
return $reflector->newInstanceArgs($instances);
}
可見(jiàn)一個(gè)服務(wù)容器就加載成功了。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel框架源碼解析之模型Model原理與用法解析
- Laravel框架源碼解析之入口文件原理分析
- Laravel 框架控制器 Controller原理與用法實(shí)例分析
- Laravel框架數(shù)據(jù)庫(kù)CURD操作、連貫操作總結(jié)
- PHP開(kāi)發(fā)框架Laravel數(shù)據(jù)庫(kù)操作方法總結(jié)
- Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類(lèi)的方法
- Laravel框架路由配置總結(jié)、設(shè)置技巧大全
- Laravel 5 框架入門(mén)(一)
- Laravel 5框架學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)遷移(Migrations)
- Laravel 5框架學(xué)習(xí)之向視圖傳送數(shù)據(jù)
- Laravel 5框架學(xué)習(xí)之用戶(hù)認(rèn)證
- Laravel框架集合用法實(shí)例淺析
相關(guān)文章
PHP htmlspecialchars() 函數(shù)實(shí)例代碼及用法大全
這篇文章主要介紹了PHP htmlspecialchars() 函數(shù)實(shí)例代碼及用法大全,需要的朋友可以參考下2018-09-09
php讀取EXCEL文件 php excelreader讀取excel文件
php開(kāi)發(fā)中肯定會(huì)遇到將excel文件內(nèi)容導(dǎo)入到數(shù)據(jù)庫(kù)的需要,php-excel-reader可以很輕松的使用它讀取excel文件,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
php 調(diào)用百度sms來(lái)發(fā)送短信的實(shí)現(xiàn)示例
這篇文章主要介紹了php 調(diào)用百度sms來(lái)發(fā)送短信的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
thinkphp6使用mysql悲觀(guān)鎖解決商品超賣(mài)問(wèn)題的實(shí)現(xiàn)
這篇文章主要介紹了thinkphp6使用mysql悲觀(guān)鎖解決商品超賣(mài)問(wèn)題的實(shí)現(xiàn)2021-11-11
重新認(rèn)識(shí)php array_merge函數(shù)
PHP中合并數(shù)組分成兩種情況:1.如果這兩個(gè)數(shù)組中有相同的字符串鍵名 2.如果這兩個(gè)數(shù)組中有相同的數(shù)值鍵名2014-08-08
Thinkphp的volist標(biāo)簽嵌套循環(huán)使用教程
這篇文章主要介紹了Thinkphp實(shí)現(xiàn)volist標(biāo)簽嵌套循環(huán)的方法,需要的朋友可以參考下2014-07-07
Thinkphp模板標(biāo)簽if和eq的區(qū)別和比較實(shí)例分析
這篇文章主要介紹了Thinkphp模板標(biāo)簽if和eq的區(qū)別和比較,實(shí)例分析了Thinkphp模板標(biāo)簽if和eq用于變量比較的區(qū)別,需要的朋友可以參考下2015-07-07
PHP編寫(xiě)學(xué)校網(wǎng)站上新生注冊(cè)登陸程序的實(shí)例分享
這篇文章主要介紹了使用PHP編寫(xiě)學(xué)校網(wǎng)站上新生注冊(cè)登陸程序的實(shí)例分享,只包括簡(jiǎn)單的數(shù)據(jù)庫(kù)信息錄入和查詢(xún)等步驟的一些關(guān)鍵點(diǎn),需要的朋友可以參考下2016-03-03

