PHP自动加载-spl_autoload_register
spl_autoload_register 自动加载
spl : Standard PHP library (标准PHP库)
首先来了解 __autoload
print.class.php
<?php
class print {
function doPrint(){
echo 'hello world';
}
}
?>
index.php
<?php
// 这段即是 autoload的自动加载
function __autoload($class){
$file = $class.'.class.php';
if (is_file($file)){
require_once($file);
}
} $obj = new print();
$obj->doPrint(); // 打印 'hello world'
?>
index.php 的代码里面如果没有 autoload那段,则会直接不能运行,因为new 的对象都找不到。
PHPstorm编辑器 会帮你找到,但是程序跑起来的时候是有问题的。
spl_autoload_register
<?php
function load($class){
$file = $class.'.class.php';
if (is_file($file)){
require_once ($file);
}
} spl_autoload_register('load');
$obj = new print();
$obj->doPrint(); // 打印 'hello world'
?>
spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] ) : bool
有三个参数
spl_autoload_register(autoload_function, [throw], [prepend])
- autoload_funtion : 欲自动装载的函数,如果没有提供的话,则自动注册autoload的默认实现spl_autoload()
- throw : 设置 autoload_function 无法成功注册的时候是否 抛出异常 (true)
- prepend : 添加函数到队列之首,而不是队列尾部 (true)
// 自 PHP 5.3.0 起可以使用一个匿名函数
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});

PHP自动加载-spl_autoload_register的更多相关文章
- PHP 命名空间与自动加载机制介绍
include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...
- PHP面向对象----- 类的自动加载
1.类的自动加载 spl_autoload_register函数 test.php <?php spl_autoload_register('autoload'); // require_onc ...
- thinkphp5源码剖析系列1-类的自动加载机制
前言 tp5想必大家都不陌生,但是大部分人都停留在应用的层面,我将开启系列随笔,深入剖析tp5源码,以供大家顺利进阶.本章将从类的自动加载讲起,自动加载是tp框架的灵魂所在,也是成熟php框架的必备功 ...
- 构建自己的PHP框架之自动加载类中详解spl_autoload_register()函数
在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...
- 自动加载类PHP中spl_autoload_register函数的用法
spl_autoload_register(PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明bool spl_autoloa ...
- php中自动加载类_autoload()和spl_autoload_register()实例详解
一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例 ...
- PHP自动加载上——spl_autoload_register
spl_autoload_register函数是实现自动加载未定义类功能的的重要方法,所谓的自动加载意思就是 我们的new 一个类的时候必须先include或者require的类文件,如果没有incl ...
- PHP自动加载(__autoload和spl_autoload_register)
一:什么是自动加载 我们在new出一个class的时候,不需要手动去require或include来导入这个class文件,而是程序自动帮你导入这个文件不需要手动的require那么多class文件了 ...
- php的自动加载函数spl_autoload_register和__autoload
spl_autoload_register和__autoload是用来自动加载类的,不用每次都require,include这样搞. 先说__autoload的用法, 在同级目录建立2个文件,一个in ...
随机推荐
- C# download big file
I had validated this.To download SSMS which is more than 500M+ static void Main(string[] args) { str ...
- C# convert between Image and Base64string
static void ImageMSDemo(string picPath) { byte[] imageArray = System.IO.File.ReadAllBytes(picPath); ...
- 让你彻底理解volatile,面试不再愁
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- Winform中怎样获取项目图片资源并转换为Image对象
场景 DevExpress的TreeList怎样给树节点设置图标: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10274554 ...
- IntelliJ IDEA UML插件
在IntelliJ IDEA Ultimate 版本中自带了一个UML插件:UMLSupport 查看了Community版本和AndroidStudio 发现没有这个插件. 要使用这个插件导出需要的 ...
- [20190913]完善vim的bccacl插件2.txt
[20190913]完善vim的bccacl插件2.txt --//继续完善vim的bccacl插件.--//\bc 计算也可以直接使用 \bb 操作,这样操作更快一些.--//增加直接写好算式计算的 ...
- 计算机基础 python安装时的常见致命错误 pycharm 思维导图
计算机基础 1.组成 人 功能 主板:骨架 设备扩展 cpu:大脑 计算 逻辑处理 硬盘: 永久储存 电源:心脏 内存: 临时储存,断电无 操作系统(windonws mac linux): 软件,应 ...
- 使用vs code编写Markdown文档以及markdown语法详解
首先安装vscode工具,下载地址如下: https://code.visualstudio.com/ 在vs code的扩展中安装: Markdown Preview Enhanced 这款插件,安 ...
- 以聚合数据免费接口为例,通过 Class 类继承方法,让小程序实现项目化接口调用
微信小程序数据来源,是通过接口实现的.但接口如何调,数据如何取?每个人都有不同的方法,下面以聚合数据免费接口为例. 配置接口 config.js 聚合数据请求接口需要以key作为参数. const c ...
- SQL server 2012 各个版本比较
有关不同版本的 SQL Server 2012 所支持的功能的详细信息. 功能名称 Enterprise 商业智能 Standard Web Express with Advanced Service ...