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必知必会之类库自动加载的七种方式(三)的更多相关文章

  1. 介绍PHP的自动加载

    昨天面试被问到了 PHP 的自动加载机制,因为很多概念模糊啦,没回答好,今天特意来总结一下. include 和 require 是PHP中引入文件的两个基本方法,但是每个脚本的开头,都需要包含(in ...

  2. composer的autoload来自动加载自己编写的函数库与类库?

    1.使用命令composer init生成composer.json文件,并编辑autoload选项内容如下: 其中又包含主要的两个选项: files 和 psr-4. files就是需要compos ...

  3. PHP 命名空间与自动加载机制介绍

    include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...

  4. composer的自动加载机制(autoload)

    composer的出现真是让人们眼前一亮,web开发从此变成了一件很『好玩』的事情,开发一个CMS就像在搭积木,从packagist中取出『积木』搭建在自己的代码中,一点一点搭建出一个属于自己的王国. ...

  5. PHP 命名空间与自动加载机制

    include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...

  6. 29)PHP,自动加载类

    (1)作用: 类的自动加载是指,在外面的页面中,并不需要去“引入”(包含)类文件,但是程序会在需要一个类的时候就自动去“动态加载”该类. (2)什么叫做“需要一个类”?通常是这样的情况: 1,创建一个 ...

  7. H5系列之History(必知必会)

    H5系列之History(必知必会)   目录 概念 兼容性 属性 方法 H5方法       概念     理解History Api的使用方式 目的是为了解决哪些问题   作用:ajax获取数据时 ...

  8. 必知必会之 Java

    必知必会之 Java 目录 不定期更新中-- 基础知识 数据计量单位 面向对象三大特性 基础数据类型 注释格式 访问修饰符 运算符 算数运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 三目表达式 ...

  9. Java面试必知必会:基础

    面试考察的知识点多而杂,要完全掌握需要花费大量的时间和精力.但是面试中经常被问到的知识点却没有多少,你完全可以用 20% 的时间去掌握 80% 常问的知识点. 一.基础 包括: 杂七杂八 面向对象 数 ...

随机推荐

  1. stimulsoft report工具—— 简单的多表连接打印报表例子

    一.用报表工具打印多个表格信息(包括学生表.教师表.班级表) 1.准备打印的数据(用sqlserver) 1)班级表

  2. Unicode与UTF-8,UTF-16

    Unicode(UTF-8, UTF-16)令人混淆的概念 为啥需要Unicode 我们知道计算机其实挺笨的,它只认识0101这样的字符串,当然了我们看这样的01串时肯定会比较头晕的,所以很多时候为了 ...

  3. 014PHP文件处理——文件指针控制fseek rewind ftell feof fpassthru

    <?php /** * 文件指针控制fseek rewind ftell feof fpassthru */ //feof()判断文件读取是否超出文件长度 /*$file = fopen('a. ...

  4. php抓取文章内容分析

    preg_match_all — 执行一个全局正则表达式匹配 int preg_match_all ( string pattern, string subject, array matches [, ...

  5. C++面向对象多线程入门

    第1节   背景 为了更好的理解多线程的概念,先对进程,线程的概念背景做一下简单介绍. 早期的计算机系统都只允许一个程序独占系统资源,一次只能执行一个程序.在大型机年代,计算能力是一种宝贵资源.对于资 ...

  6. <NET CLR via c# 第4版>笔记 第18章 定制特性

    18.1 使用定制特性 FCL 中的几个常用定制特性. DllImport 特性应用于方法,告诉 CLR 该方法的实现位于指定 DLL 的非托管代码中. Serializable 特性应用于类型,告诉 ...

  7. 不同数据库的driverClassName与url

    # Properties file with JDBC-related settings. ########## # HSQLDB # ########## #jdbc.driverClassName ...

  8. java爬虫爬取的html内容中空格(&nbsp;)变为问号“?”的解决方法

    用java编写的爬虫,使用xpath爬取内容后,发现网页源码中的 全部显示为?(问号),但是使用字符串的replace("?", ""),并不能替换,网上找了一 ...

  9. 子域名爆破&C段查询&调用Bing查询同IP网站

    在线子域名爆破 <?php function domainfuzz($domain) { $ip = gethostbyname($domain); preg_match("/\d+\ ...

  10. UITableView简述

    原帖:http://blog.csdn.net/totogo2010/article/details/7642908 Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Ta ...