php的自动加载函数spl_autoload_register和__autoload
spl_autoload_register和__autoload是用来自动加载类的,不用每次都require,include这样搞。
先说__autoload的用法,
在同级目录建立2个文件,一个index.php,一个是test.php,内容。
test.php
<?php class Test{
function sayhi(){
echo "hi";
}
}
?>
index.php
<?php //第一种
function __autoload($class){
$file = $class.'.php'; if(is_file($file)){
require_once ($file);
}
} $o = new Test();
$o->sayhi(); ?>
这样执行,在index.php找不到test类的时候,会自动执行__autoload函数,把test类加载进来,并且实例化,输出hi
接下来讲spl_autoload_register。也是上面那个test.php,index.php如下
function loadscript($class){
$file = $class.'.php';
if(is_file($file)){
require_once ($file);
}
}
spl_autoload_register('loadscript');
$o = new Test();
$o->sayhi();
这里注意下,找不到Test类之后,会去执行spl_autoload_register ,注册的函数loadscript,接着把test类加载进来。
同时,可以把loadscript函数写在类的静态方法中,这样也能自动加载
//第三
class m {
public static function loadprint( $class ) {
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
} spl_autoload_register(array('m','loadprint'));
$o = new Test();
$o->sayhi();
自己参考上面写一遍都知道了~
php的自动加载函数spl_autoload_register和__autoload的更多相关文章
- ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...
- PHP自动加载上——spl_autoload_register
spl_autoload_register函数是实现自动加载未定义类功能的的重要方法,所谓的自动加载意思就是 我们的new 一个类的时候必须先include或者require的类文件,如果没有incl ...
- php注册自动加载函数
$autoload_func = function($class) { $class = str_replace('\\', '/', $class); $file_name = dirname(__ ...
- php自动加载函数
含义:将函数注册到SPL __autoload函数栈中.如果该栈中的函数尚未激活,则激活它们. 先看__autoload 函数 printit.class.php <?php class PRI ...
- spl_autoload_register()怎样注册多个自动加载函数?
<?php /*function __autoload($class){ require("./class/".$class.".php"); }*/ f ...
- spl_autoload_register 实现自动加载
spl_autoload_register 注册给定的函数作为 __autoload 的实现 bool spl_autoload_register ([ callable $autoload_func ...
- 如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题
function aotuman($class){ include('./'.$class.'.class.php'); } spl_autoload_register('automan'); / ...
- laravel框架源码分析(一)自动加载
一.前言 使用php已有好几年,laravel的使用也是有好长时间,但是一直对于框架源码的理解不深,原因很多,归根到底还是php基础不扎实,所以源码看起来也比较吃力.最近有时间,所以开启第5.6遍的框 ...
- 构建自己的PHP框架之自动加载类中详解spl_autoload_register()函数
在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...
随机推荐
- http 502 bad gate way
世界杯期间,公司的cdn在回源时突然出现大量502. 刚出现问题时,因为考虑到一般502都是上游服务器出现问题,然后因为已经服务了很久都没有出现过这种问题, 就没有仔细考虑,就让回源中心的同事进行排查 ...
- Could not resolve placeholder 'jdbc.url' in value "${jdbc.url}"
写完接口之后,发现报了这个错误,查了一下发现,spring不允许使用两个 <context:property-placeholder>
- 第九次ScrumMeeting博客
第九次ScrumMeeting博客 本次会议于11月4日(六)22时整在3公寓725房间召开,持续20分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的 ...
- 【探路者】Alpha发布用户使用报告
预期统计用户使用数量:13人. 博文内容:1用户列表.2评论列表.3统计与总结 1用户列表: 二.评论内容 用户1:1不够好看.2不应该是中国地图为背景,蛇头是人物头像的么?(那是宣传片,不是预览图) ...
- BETA阶段冲刺
1.介绍小组新加入的成员,Ta担任的角色 新成员 担任工作 江鹭涛 前端设计 2.讨论是否需要更换团队的PM 不需要,上阶段配合不错,这阶段继续努力 3.下一阶段需要改进完善的功能 服务器并发处理,界 ...
- 解决:Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986
目录 背景 原因分析 处理方法 参考 背景 在将tomcat升级到7.0.81版后,发现系统的有些功能不能使用了,查询日志发现是有些地址直接被tomcat认为存在不合法字符,返回HTTP 400错误响 ...
- Java clone() 浅拷贝 深拷贝
假如说你想复制一个简单变量.很简单: int apples = 5; int pears = apples; 不仅仅是int类型,其它七种原始数据类型(boolean,char,byte,short, ...
- 高性能的HTTP代理 LittleProxy
引用: https://github.com/adamfisk/LittleProxy 拦截和操纵HTTPS流量,LittleProxy使用中间人(MITM)管理器. LittleProxy的默认实现 ...
- mybatis 批量插入 返回主键id
我们都知道Mybatis在插入单条数据的时候有两种方式返回自增主键: 1.对于支持生成自增主键的数据库:增加 useGenerateKeys和keyProperty ,<insert>标签 ...
- 第94天:CSS3 盒模型详解
CSS3盒模型详解 盒模型设定为border-box时 width = border + padding + content 盒模型设定为content-box时 width = content所谓定 ...