__autoload()和spl_autoload_register()
2011-03-27 18:00
169 查看
前者直接加载包含文件,后者指定函数加载包含文件。
<?php
//DIRECTORY_SEPARATOR当前系统的目录分隔符
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
//定义站点根目录
if (!defined('SMARTY_DIR')) {
define('SMARTY_DIR', dirname(__FILE__) . DS);
}
//定义引入类库目录
if (!defined('MYINCLUDE_DIR')) {
define('MYINCLUDE_DIR', SMARTY_DIR . 'include' . DS);
}
//get_include_path()为引入文件目录列表
//set_include_path()向原目录添加新的调用目录
//PATH_SEPARATOR 为系统常量,目录列表间的分隔符
set_include_path(get_include_path().PATH_SEPARATOR.MYINCLUDE_DIR);
//指定引入新类时,通过调用的函数为loadclass
spl_autoload_register('loadclass');
//load_substr,外部类
$ls=new load_substr("abcdefg");
echo $ls->sub_str2(3);
//spl_autoload_register()方式调用
function loadclass($classname){
$file_path=MYINCLUDE_DIR.$classname.'.php';
if(is_file($file_path)){
include $file_path;
}
}
//__autoload()方式调用
function __autoload($classname){
$file_path=MYINCLUDE_DIR.$classname.'.php';
if(is_file($file_path)){
include $file_path;
}
}
//当__autoload()和spl_autoload_register()同时使用时,程序将优先使用后者。
?>
include/load_substr.php
<?php
class load_substr{
public $substr='';
public function __construct($str){
$this->substr=$str;
}
public function sub_str2($num){
return substr($this->substr,0,$num);
}
}
?>
<?php
//DIRECTORY_SEPARATOR当前系统的目录分隔符
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
//定义站点根目录
if (!defined('SMARTY_DIR')) {
define('SMARTY_DIR', dirname(__FILE__) . DS);
}
//定义引入类库目录
if (!defined('MYINCLUDE_DIR')) {
define('MYINCLUDE_DIR', SMARTY_DIR . 'include' . DS);
}
//get_include_path()为引入文件目录列表
//set_include_path()向原目录添加新的调用目录
//PATH_SEPARATOR 为系统常量,目录列表间的分隔符
set_include_path(get_include_path().PATH_SEPARATOR.MYINCLUDE_DIR);
//指定引入新类时,通过调用的函数为loadclass
spl_autoload_register('loadclass');
//load_substr,外部类
$ls=new load_substr("abcdefg");
echo $ls->sub_str2(3);
//spl_autoload_register()方式调用
function loadclass($classname){
$file_path=MYINCLUDE_DIR.$classname.'.php';
if(is_file($file_path)){
include $file_path;
}
}
//__autoload()方式调用
function __autoload($classname){
$file_path=MYINCLUDE_DIR.$classname.'.php';
if(is_file($file_path)){
include $file_path;
}
}
//当__autoload()和spl_autoload_register()同时使用时,程序将优先使用后者。
?>
include/load_substr.php
<?php
class load_substr{
public $substr='';
public function __construct($str){
$this->substr=$str;
}
public function sub_str2($num){
return substr($this->substr,0,$num);
}
}
?>
相关文章推荐
- 详解spl_autoload_register()函数
- spl_autoload_register
- PHP中spl_autoload_register()函数的用法
- 深入理解 spl_autoload , __autoload 以及 spl_autoload_register
- 关于spl_autoload_register()
- 详解spl_autoload_register() 与 __autoload
- 关于php 中spl_autoload_register的一些补充
- 详解spl_autoload_register()函数以及自动加载不同目录的类
- PHP 命名空间与自动加载机制介绍,spl_autoload_register, __autoload 函数
- php命名空间+spl_autoload_register实现类的自动加载
- php spl_autoload_register函数接口
- 使用spl_autoload_register实现自动加载
- php中spl_autoload_register和autoload
- PHP中spl_autoload_register()和__autoload()区别
- php 加载函数 __autoload(), spl_autoload_register()
- PHP spl_autoload_register()函数的一些见解
- spl_autoload_register()函数
- spl_autoload_register和__autoload
- spl_autoload_register 注册自己的自动加载函数(__autoload())
- spl_autoload_register与__autoload区别