phper必知必会之类库自动加载的七种方式(三)
生活随笔
收集整理的這篇文章主要介紹了
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必知必会之类库自动加载的七种方式(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sign check fail: che
- 下一篇: python源码剖析_Python源码剖