含义:将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。
先看__autoload 函数

printit.class.php
<?php
class PRINTIT {
function doPrint() {
echo 'hello world';
}
}
?> index.php
<?
function __autoload( $class ) {
$file = $class . '.class.php';
if ( is_file($file) ) {
require_once($file);
}
} $obj = new PRINTIT();
$obj->doPrint();
?>

在index.php中,由于没有包含printit.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时printit.class.php就被引进来了。

<?
function loadprint( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
} spl_autoload_register( 'loadprint' ); $obj = new PRINTIT();
$obj->doPrint();

将__autoload换成loadprint函数。但是loadprint不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行loadprint()。

调用静态方法

<?
class test {
public static function loadprint( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}
} spl_autoload_register( array('test','loadprint') );
//另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT();
$obj->doPrint();
?>

php自动加载函数的更多相关文章

  1. ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)

    一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...

  2. php的自动加载函数spl_autoload_register和__autoload

    spl_autoload_register和__autoload是用来自动加载类的,不用每次都require,include这样搞. 先说__autoload的用法, 在同级目录建立2个文件,一个in ...

  3. php注册自动加载函数

    $autoload_func = function($class) { $class = str_replace('\\', '/', $class); $file_name = dirname(__ ...

  4. spl_autoload_register()怎样注册多个自动加载函数?

    <?php /*function __autoload($class){ require("./class/".$class.".php"); }*/ f ...

  5. 构建自己的PHP框架之自动加载类中详解spl_autoload_register()函数

    在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...

  6. PHP 命名空间以及自动加载(自动调用的函数,来include文件)

    这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...

  7. php自动加载的两个函数__autoload和__sql_autoload_register

    一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php //文件 <?php class P ...

  8. 如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题

    function aotuman($class){  include('./'.$class.'.class.php'); } spl_autoload_register('automan');  / ...

  9. 【php】命名空间 和 自动加载的关系

    目的 本文的目的主要是说明 命名空间的 use 关键词 和 new ClassName 这两个步骤,哪个步骤才会执行自动加载,这是逻辑有点混乱的表现,这种想法也是很正常的,让我们来解密吧 命名空间(n ...

随机推荐

  1. 2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分

    ①登陆   注册    查表匹配    0405Ajax.php   ②判断用户名是否存在 <!DOCTYPE html> <html lang="en"> ...

  2. Hackrank Equal DP

    Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. ...

  3. 超过经理收入的员工 表的自JOIN

    https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/description/ The Employe ...

  4. I.MX6 查找占用UART进程

    /**************************************************************************** * I.MX6 查找占用UART进程 * 说 ...

  5. python-----贴图 和 报错:OSError: image file is truncated (8 bytes not processed)的处理

    将一张图片贴到另一张图片上,代码如下: from PIL import Image import os from PIL import ImageFile ImageFile.LOAD_TRUNCAT ...

  6. Gerrit配置--用户配置

    环境: Gerrit Server:172.16.206.133 Client:172.16.206.129 1.在Gerrit服务器上创建用户 Gerrit服务器使用的是HTTP认证类型,并用htt ...

  7. ubuntu16.04下使用python3开发时,安装pip3与scrapy,升级pip3

    1)安装pip3: sudo apt-get install python3-pip 2)安装scrapy sudo pip3 install scrapy 若出现版本过低问题: pip3 insta ...

  8. redis发布(pub)、订阅(sub)模式

    前言:redis提供了很多种功能或模式,可以运用在不同的场景下,今天记录下redis中的发布.订阅模式的基本使用 注redis安装及主从搭建请参考我其他博文http://www.cnblogs.com ...

  9. E20170524-gg

    Awesome   adj. 可怕的; 令人敬畏的; 使人畏惧的; 极好的;

  10. Android Studio编写运行测试纯java代码可带main()函数

    问题 小伙伴们在做安卓项目的时候,是不是有时候会忘记某些api的使用方法,不太确定他们的结果是怎样的,需要写一些测试代码,验证看看我们的写法是否正确.刚开始的时候我是在页面上写一个Button,添加点 ...