日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php manual 反射,Laravel框架源码解析之反射的使用详解

發(fā)布時(shí)間:2025/3/21 php 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php manual 反射,Laravel框架源码解析之反射的使用详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

前言

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

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

反射

反射類是PHP內(nèi)部類,無需加載即可使用,你可以通過實(shí)例化 ReflectionClass類去使用它。

方法

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

方法名

注釋

ReflectionClass::getConstant

獲取定義過的一個(gè)常量

ReflectionClass::getConstants

獲取一組常量

ReflectionClass::getConstructor

獲取類的構(gòu)造函數(shù)

ReflectionClass::getDefaultProperties

獲取默認(rèn)屬性

ReflectionClass::getDocComment

獲取文檔注釋

ReflectionClass::getEndLine

獲取最后一行的行數(shù)

ReflectionClass::getFileName

獲取定義類的文件名

ReflectionClass::getInterfaceNames

獲取接口(interface)名稱

ReflectionClass::getMethods

獲取方法的數(shù)組

ReflectionClass::getModifiers

獲取類的修飾符

ReflectionClass::getName

獲取類名

ReflectionClass::getNamespaceName

獲取命名空間的名稱

ReflectionClass::getParentClass

獲取父類

等等等等.... 所有關(guān)于類的方法、屬性及其繼承的父類、實(shí)現(xiàn)的接口都可以查詢到。

詳細(xì)文檔請參考官網(wǎng): http://php.net/manual/zh/class.reflectionclass.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í)使用了反射類。現(xiàn)在我們開啟“解刨”模式

入口文件

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ā)生的下一行調(diào)用了make方法。各位很清楚,make方法用于解析類,所有make方法的實(shí)現(xiàn)一定是在引用的文件內(nèi)。

bootstrap\app.php

$app = new Illuminate\Foundation\Application(

realpath(__DIR__.'/../')

);

laravel開始加載它的核心類,所有的實(shí)現(xiàn)從 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);

}

在核心類中你可能準(zhǔn)確的查找到make方法的存在,它加載了服務(wù)提供者隨后調(diào)用了父類的方法make,要知道作為獨(dú)立的模塊 “服務(wù)容器”是絕對不能寫在核心類的。懂點(diǎn)設(shè)計(jì)模式的都很清楚。

Illuminate\Container\Container

以$api = $this->app->make('HelpSpot\API',['id'=>1]);為例來講解

// 真正的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í)例化,而是通過反射“解刨”了)

$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í)例化反射類

$reflector = new ReflectionClass($concrete);

// 檢查類是否可實(shí)例化

if (! $reflector->isInstantiable()) {

return $this->notInstantiable($concrete);

}

$this->buildStack[] = $concrete;

// 獲取類的構(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è)新的類實(shí)例。

return $reflector->newInstanceArgs($instances);

}

可見一個(gè)服務(wù)容器就加載成功了。

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

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的php manual 反射,Laravel框架源码解析之反射的使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。