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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

phper必知必会之类库自动加载的七种方式(三)

發布時間:2023/12/10 php 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 phper必知必会之类库自动加载的七种方式(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

## php自動加載

下面顯示例子的文件目錄結構圖

一、沒有使用命名空間的幾種實現

test/oneClass.php

class oneClass{public function show(){echo "這里是oneClass.php的show方法<br/>";}}

test/twoClass.php

<?phpclass twoClass{public function show(){echo "這里是twoClass.php的show方法<br/>";}}

下面7種方式都可以實現自動加載,結果都為:

這里是oneClass.php的show方法 這里是twoClass.php的show方法

方法一:index.php 使用__autoload()魔術方法實現自動加載

<?php //7.2以后使用這個提示一個警告,Deprecated: __autoload() is deprecated, use spl_autoload_register() instead function __autoload($classname){include './test/'.$classname.'.php'; }//調用類庫如果找不到會自動執行__autoload() $one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

運行結果

Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /Users/lidong/Desktop/wwwroot/test/April/autoload1/index.php on line 5 這里是oneClass.php的show方法 這里是twoClass.php的show方法

總結:在PHP7.2以后使用__autoload()會報一個警告,7.2之前這種方式是沒提示的.這種方式,是調用一個找不到的類會自動取調用__autoload()方法然后在方法里面執行include引用,實現自動加載。

方法二:index2.php 使用spl_autoload_register()方法實現自動加載,創建自定義register方法調用

<?phpfunction register($classname){include "./test/{$classname}.php"; }spl_autoload_register("register");$one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

方法三:index3.php 使用spl_autoload_register()方法,不定義register方法直接使用回調

<?phpspl_autoload_register(function($classname){include "./test/{$classname}.php"; });$one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

方法四:index4.php 使用spl_autoload_register()方法,調用類的register方法實現自動加載

class autoLoad{public static function register($classname){include "./test/{$classname}.php";} }spl_autoload_register(["autoLoad","register"]);$one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

二、使用命名空間的幾種實現

test2/oneClass.php

<?phpnamespace auto\test2; class oneClass{public function show(){echo "這里是oneClass.php的show方法<br/>";}}

test2/twoClass.php

<?php namespace auto\test2; class twoClass{public function show(){echo "這里是twoClass.php的show方法<br/>";}}

方法五:index5.php,使用spl_autoload_register(),調用加載類的register方法,轉化傳遞過來的命名空間實現自動加載

<?phpclass autoLoad{public static function register($classname){$arr = explode('\\', $classname);include "./test2/{$arr[2]}.php";} }spl_autoload_register(["autoLoad","register"]);$one = new auto\test2\oneClass(); $one->show(); $two = new auto\test2\twoClass(); $two->show();

方法六:index6.php 跟方法五類似,區別是use方法調用類實例化時可以直接使用類名,實現自動加載

<?phpuse auto\test2\oneClass; use auto\test2\twoClass;class autoLoad{public static function register($classname){$arr = explode('\\', $classname);include "./test2/{$arr[2]}.php";} }spl_autoload_register(["autoLoad","register"]);$one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

方法七:index7.php 與方法五和六思路一致,只不過加載類放在外部不是引用在統一文件,要點就是命名空間定義的類,要使用也要先include,實現自動加載

autoLoad.php
<?phpnamespace auto; class autoLoad{public static function register($classname){$arr = explode('\\', $classname);include "./test2/{$arr[2]}.php";} }
index7.php
<?php use auto\test2\oneClass; use auto\test2\twoClass;include "./autoLoad.php";spl_autoload_register(["auto\autoLoad","register"]);$one = new oneClass(); $one->show(); $two = new twoClass(); $two->show();

總結:所有的自動加載思想都是調用一個沒引用的類庫會,PHP會自動執行的一個加載方法,這個方法有可能是類的方法也有可能是普通方法,但不管怎么樣都最終使用include執行文件包含,只不過命名空間需要轉化下獲取類名。另外值得注意的是,如果是一個php的框架自動加載實現也基本一致,只不過他會根據不同文件夾下面的定義判斷后include來實現不同文件夾下文件的引用,來實現整個框架的自動加載。

轉載于:https://www.cnblogs.com/lisqiong/p/10763793.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的phper必知必会之类库自动加载的七种方式(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人小片| 无码人妻精品一区二区 | 亚洲av无码乱码国产精品fc2 | 91精品福利在线 | 日本视频在线免费观看 | 亚洲天堂av电影 | 国产免费aa | 国产在线伊人 | 成年视频在线 | 日本黄色三级网站 | 嫩模一区 | 亚洲网址在线 | 中文字幕亚洲综合 | 色多多视频在线观看 | 欧美色拍 | 免费污片网站 | 免费观看一区二区三区视频 | 午夜黄色福利 | 日韩福利视频 | 欧美日韩亚洲在线 | 久久网免费视频 | 99福利在线| 国产高清自拍av | av中文在线资源 | 国产精品系列在线观看 | 国产精品色婷婷99久久精品 | 激情亚洲天堂 | 亚洲天堂v | 国产精品露脸视频 | 久久青青 | 成人欧美一区二区三区在线播放 | 三年中国片在线高清观看 | 伊人网av在线 | 青青操国产视频 | 黄色日批| 张柏芝54张无删码视频 | 精品一区二区视频在线观看 | 青青草原在线免费观看视频 | 欧美s码亚洲码精品m码 | 小视频免费在线观看 | 亚洲色图插插插 | 久久国产免费看 | 三浦惠理子aⅴ一二三区 | 国产av成人一区二区三区 | 91日本在线观看 | 成人av激情| 欧美黄色大片视频 | 美女av免费在线观看 | 人日人视频 | 天天干天天爽天天射 | 国产夜夜操| 欧美字幕| 最新精品在线 | 貂蝉被到爽流白浆在线观看 | 久久久久久少妇 | 欧美激情综合五月色丁香 | 男人天堂视频网站 | 欧美第一视频 | 欧美日韩免费高清一区色橹橹 | 网站久久久 | 99资源站| 肥臀熟女一区二区三区 | 美女露胸露尿口 | 伊人中文在线 | 国产免费一级 | 天天摸天天插 | 激情网站在线观看 | 亚洲天堂麻豆 | 四虎成人在线观看 | 天天操狠狠操 | 宅宅少妇无码 | 免费看黄色片网站 | 黄色自拍视频 | 亚洲精品国产成人久久av盗摄 | 97se.com| 小草av在线 | 亚洲你我色 | 91精品999| 日韩在线免费视频 | 日韩午夜影院 | 性生活一区 | 欧美性猛交xxxx久久久 | 国产黄色片免费观看 | 日韩高清在线一区 | 一级黄视频 | 一区二区三区爱爱 | 国产精品久久伊人 | xxx精品| 午夜一级大片 | caoporn成人| 中文字幕在线观看国产 | 国产午夜在线播放 | 一区二区高清在线观看 | 国产寡妇亲子伦一区二区三区四区 | 你懂的在线网站 | 亚洲色图19p | 色综合天天色 | 久久日韩精品 | 亚洲手机在线观看 |