phper必知必会之类库自动加载的七种方式(三)
php自动加载
下面显示例子的文件目录结构图

一、没有使用命名空间的几种实现
test/oneClass.php
class oneClass{
public function show(){
echo "这里是oneClass.php的show方法<br/>";
}
}
test/twoClass.php
<?php
class 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方法调用
<?php
function 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方法直接使用回调
<?php
spl_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
<?php
namespace 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方法,转化传递过来的命名空间实现自动加载
<?php
class 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方法调用类实例化时可以直接使用类名,实现自动加载
<?php
use 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
<?php
namespace 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来实现不同文件夹下文件的引用,来实现整个框架的自动加载。
phper必知必会之类库自动加载的七种方式(三)的更多相关文章
- 介绍PHP的自动加载
昨天面试被问到了 PHP 的自动加载机制,因为很多概念模糊啦,没回答好,今天特意来总结一下. include 和 require 是PHP中引入文件的两个基本方法,但是每个脚本的开头,都需要包含(in ...
- composer的autoload来自动加载自己编写的函数库与类库?
1.使用命令composer init生成composer.json文件,并编辑autoload选项内容如下: 其中又包含主要的两个选项: files 和 psr-4. files就是需要compos ...
- PHP 命名空间与自动加载机制介绍
include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...
- composer的自动加载机制(autoload)
composer的出现真是让人们眼前一亮,web开发从此变成了一件很『好玩』的事情,开发一个CMS就像在搭积木,从packagist中取出『积木』搭建在自己的代码中,一点一点搭建出一个属于自己的王国. ...
- PHP 命名空间与自动加载机制
include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...
- 29)PHP,自动加载类
(1)作用: 类的自动加载是指,在外面的页面中,并不需要去“引入”(包含)类文件,但是程序会在需要一个类的时候就自动去“动态加载”该类. (2)什么叫做“需要一个类”?通常是这样的情况: 1,创建一个 ...
- H5系列之History(必知必会)
H5系列之History(必知必会) 目录 概念 兼容性 属性 方法 H5方法 概念 理解History Api的使用方式 目的是为了解决哪些问题 作用:ajax获取数据时 ...
- 必知必会之 Java
必知必会之 Java 目录 不定期更新中-- 基础知识 数据计量单位 面向对象三大特性 基础数据类型 注释格式 访问修饰符 运算符 算数运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 三目表达式 ...
- Java面试必知必会:基础
面试考察的知识点多而杂,要完全掌握需要花费大量的时间和精力.但是面试中经常被问到的知识点却没有多少,你完全可以用 20% 的时间去掌握 80% 常问的知识点. 一.基础 包括: 杂七杂八 面向对象 数 ...
随机推荐
- k8s集群搭建指南
一.简介 Ansible Docker Docker compose,docker swarm,docker machine Mesos,marathon Kubernetes(占据80%的市场) D ...
- 树状数组Lowbit用法
刚学树状数组,看到这里的时候懵了.经过询问,发现,原来在程序运行时,数据用的都是补码,于是解决了 int Lowbit(x) { return x&(-x); } 如: x =1: 1 &am ...
- 关于c++显示调用析构函数的陷阱
版权声明:欢迎转载,注明出处就好!如果不喜欢请留言说明原因再踩哦,谢谢,我也可以知道原因,不断进步!! 目录(?)[+] 一.文章来由 现在在写一个项目,需要用到多叉树存储结构,但是在某个时候 ...
- 使用MyEclipse开发Java EE应用:企业级应用程序项目(下)
你开学,我放价!MyEclipse线上狂欢继续!火热开启中>> [MyEclipse最新版下载] 二.项目组织.依赖性和类解析 JEE规范为企业应用程序定义了一个分层的Java类解决策略, ...
- jenkins自动化打包部署
请参考: http://m.blog.csdn.net/article/details?id=50518959 1.启动 jenkins.war ,打开首页 192.168.158.129:8080 ...
- SharePoint 2013的100个新功能之内容管理(二)
一:数据视图 SharePoint2013中的数据视图更多的是作为多项目编辑的视图.数据视图在列表项目区域打开项目,选择停止编辑时保存项目的更改.在自定义列表中,你可以选择编辑项目,它会以数据视图编辑 ...
- 2019.1.3 WLAN 802.11 a/b/g PHY Specification and EDVT Measurement I - Transmit Power Level
This lecture provides the WLAN hardware engineer the essential knowledge of IEEE 802.11 a/b/g physic ...
- CODE大全大量Flash网站收藏
我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. http://www.wallop.com在 ...
- 前端开发 —— google chart 的使用
1. 引入所需的 js 库 在 <head></head>中 <script src="https://ajax.googleapis.com/ajax/lib ...
- 如何安装Magento插件
Magento有着非常多的插件,其实就是模块,那么怎么安装需要的插件呢? 具体方法如下,以安装DeveloperToolbar这个开发插件为例讲解: 1.首先到Magento的官方网站查找到相应的插件 ...