php自动加载函数
含义:将函数注册到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自动加载函数的更多相关文章
- ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...
- php的自动加载函数spl_autoload_register和__autoload
spl_autoload_register和__autoload是用来自动加载类的,不用每次都require,include这样搞. 先说__autoload的用法, 在同级目录建立2个文件,一个in ...
- php注册自动加载函数
$autoload_func = function($class) { $class = str_replace('\\', '/', $class); $file_name = dirname(__ ...
- spl_autoload_register()怎样注册多个自动加载函数?
<?php /*function __autoload($class){ require("./class/".$class.".php"); }*/ f ...
- 构建自己的PHP框架之自动加载类中详解spl_autoload_register()函数
在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...
- PHP 命名空间以及自动加载(自动调用的函数,来include文件)
这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...
- php自动加载的两个函数__autoload和__sql_autoload_register
一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php //文件 <?php class P ...
- 如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题
function aotuman($class){ include('./'.$class.'.class.php'); } spl_autoload_register('automan'); / ...
- 【php】命名空间 和 自动加载的关系
目的 本文的目的主要是说明 命名空间的 use 关键词 和 new ClassName 这两个步骤,哪个步骤才会执行自动加载,这是逻辑有点混乱的表现,这种想法也是很正常的,让我们来解密吧 命名空间(n ...
随机推荐
- IIS7添加虚拟目录映射另一台服务器的共享文件夹
现状: 一台Windows Server2008 Web服务器 A,一台Windows Server2003 文件服务器 B,需要在A中IIS添加对B的Web访问 方法: 1.A中添加和B相同的账号, ...
- CXF拦截器(Interceptor)LoggingInInterceptor
Interceptor是CXF架构中一个重要的功能.你可以在不对核心模块进行修改的情况下,动态添加很多功能(你可以想象Struts2拦截器的优点).这对于CXF这个以处理消息为中心的服务框架来说是非常 ...
- CMMI Institute Conference 2014中国大会
我在大会上做SPD(Strategic Policy Deployment战略部署策略)的演讲,和来自各个公司的高管进行了热烈的讨论.获得好评. 有兴趣的朋友能够点击下面链接:Stratehttp:/ ...
- poj2154Color polya定理+欧拉函数优化
没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...
- android NDK 使用(多个)静态库生成动态库
android NDK 使用(多个)静态库生成动态库. 1.编写Android.mk文件:如下两种方式都可以,用于NDK编译工具生成的两个.a文件来生成最终的libtwolib-second.so动态 ...
- 基于 jQuery 的专业 ASP.NET WebForms/MVC 控件库!
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling
ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling ...
- I.MX6 bq27441 GPOUT interrupt
/******************************************************************** * I.MX6 bq27441 GPOUT interrup ...
- 【196】Dell 移动工作站系统安装方法
会出现找不到硬盘的情况,解决方法:安装系统的时候需要加载阵列卡驱动 下载阵列卡驱动,以 Dell T7610 为例根据安装系统进行选择,地址:http://zh.community.dell.com/ ...
- 05_传智播客iOS视频教程_第一个OC程序
Cocoa Application开发的是带界面的程序. OC是完全兼容C语言的,但是C语言里面是不能写OC的东西的. OC和C的第一个区别,就是源文件的后缀名的区别.OC程序的源文件的后缀名是.m, ...