php项目中类的自动加载实例讲解

yipeiwu_com5年前PHP代码库

主要函数:spl_autoload_register() — 注册给定的函数作为 __autoload() 的实现

将函数注册到SPL __autoload函数队列中。如果该队列中的函数尚未激活,则激活它们。

如果在你的程序中已经实现了__autoload()函数,它必须显式注册到__autoload()队列中。因为spl_autoload_register()函数会将Zend Engine中的__autoload()函数取代为spl_autoload()或spl_autoload_call()。

如果需要多条 autoload 函数,spl_autoload_register() 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload() 只可以定义一次。

<?php

// $class 类名
function autoloader_1($class) {
  include 'classes/' . $class . '.class.php';
}

function autoloader_2($class) {
  include 'classes/' . $class . '.class.php';
}

// 可以多次使用,但 __autoload() 函数只能使用一次。
spl_autoload_register('autoloader_1');
spl_autoload_register('autoloader_2');

// 或者,自 PHP 5.3.0 起可以使用一个匿名函数
spl_autoload_register(function ($class) {
  include 'classes/' . $class . '.class.php';
});

以上就是全部相关知识点内容,感谢大家的学习和对【宜配屋www.yipeiwu.com】的支持。

相关文章

php 强制下载文件实现代码

复制代码 代码如下:<?php $file = 'monkey.gif'; if (file_exists($file)) {     header(...

Json_decode 解析json字符串为NULL的解决方法(必看)

从APP端或从其他页面post,get过来的数据一般因为数组形式。因为数组形式不易传输,所以一般都会转json后再发送。本以为发送方json_encode(),接收方json_decod...

php 策略模式原理与应用深入理解

本文实例讲述了php 策略模式原理与应用。分享给大家供大家参考,具体如下: 策略模式 简单理解就是 有n个做法供你选择,根据你的需要选择某个策略得到结果 就应用场景来说: 例1:比如购买...

php 魔术方法使用说明

PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!!1.__construct(...

php设计模式 Observer(观察者模式)

复制代码 代码如下: <?php /** * 观察者模式 * * 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新 * 能够...