详解PHP实现自动加载的两种方式是怎样的
Admin 2022-09-17 群英技术资讯 652 次浏览
这篇文章主要讲解了“详解PHP实现自动加载的两种方式是怎样的”,文中的讲解内容简单、清晰、详细,对大家学习或是工作可能会有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。PHP实现自动加载,有两种方法:
①魔术函数 __autoload()
②spl扩展 spl_autoload_register
分别举例说明:
一、__autoload
printit.class.php:
1 <?php
2 class PRINTIT {
3 function doPrint() {
4 echo 'hello world';
5 }
6 }
index.php:
<?php
function __autoload( $class ) {
$file = $class . '.class.php';
if ( is_file($file) ) {
require_once($file);
}
}
$obj = new PRINTIT();
$obj->doPrint();
当我们每次运行index.php后都会正常输出hello world,尽管我们并没有在index.php中引入printit.class.php文件,但是我们依然能够用PRINTIT类内的方法。
在index.php中,由于没有包含printit.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时printit.class.php就被引进来了。
在面向对象中这种方法经常使用,可以避免书写过多的引用文件,同时也使整个系统更加灵活。
二、spl_autoload_register()
<?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()。
spl_autoload_register() 调用静态方法。
<?php
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();
注:SPL是Standard PHP Library(标准PHP库)的缩写。
它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。
SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。
SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。
classLoad
{
function static loadClass($class_name){
$filename= $class_name.".class.php";
$path= "include/".$filename
if(is_file($path)) returninclude$path;
}
}
/**
* 设置对象的自动载入
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass'));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( '__autoload');
如果同时用spl_autoload_register注册了一个类的方法和__autoload函数,那么,会根据注册的先后,如果在第一个注册的方法或函数里加载了类文件,就不会再执行第二个被注册的类的方法或函数。
反之就会执行第二个被注册的类的方法或函数。
<?php
class autoloader {
public static $loader;
public static function init() {
if (self::$loader == NULL)
self::$loader = new self ();
return self::$loader;
}
public function __construct() {
spl_autoload_register ( array ($this, 'model' ) );
spl_autoload_register ( array ($this, 'helper' ) );
spl_autoload_register ( array ($this, 'controller' ) );
spl_autoload_register ( array ($this, 'library' ) );
}
public function library($class) {
set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );
spl_autoload_extensions ( '.library.php' );
spl_autoload ( $class );
}
public function controller($class) {
$class = preg_replace ( '/_controller$/ui', '', $class );
set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );
spl_autoload_extensions ( '.controller.php' );
spl_autoload ( $class );
}
public function model($class) {
$class = preg_replace ( '/_model$/ui', '', $class );
set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );
spl_autoload_extensions ( '.model.php' );
spl_autoload ( $class );
}
public function helper($class) {
$class = preg_replace ( '/_helper$/ui', '', $class );
set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );
spl_autoload_extensions ( '.helper.php' );
spl_autoload ( $class );
}
}
//call
autoloader::init ();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP延迟静态绑定怎么样使用?对于“延迟静态绑定”的用法很多人不是很清楚,下面通过一个实例给大家介绍一下PHP延迟静态绑定怎样用,感兴趣的朋友就继续往下看吧。
在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。
在thinkphp中,import方法用于类库导入的封装实现,可以对项目类库、扩展类库和第三方类库提供导入支持,语法为“import('类库名', '起始路径', '类库后缀')”;该方法有个别名vendor方法,专门用于导入第三方类库。
基本的赋值运算符是“=”。一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。赋值运算表达式的值也就是所赋的值。也就是说,...
在本篇文章里我们给大家分享的是关于PHP将字符串转换成数字的方法和相关知识点,有兴趣的朋友们学习下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008