PHP自动加载类

PHP自动加载类

test.php
  1. <?php  
  2. function __autoload($class_name)   
  3. {  
  4.     require_once $class_name . '.php';  
  5. }  
  6. $obj  = new j();  
  7.   
  8. ?>   

 

当前目录下有j.php

  1. <?php  
  2. class j  
  3. {  
  4.     function __construct()   
  5.     {  
  6.     echo "成功加载";  
  7.     }   
  8. }  
  9. ?>  


正常输出:成功加载

 

修改test.php代码

  1.  <?php  
  2. function __autoload($class_name)   
  3. {  
  4.     require_once $class_name . '.php';  
  5. }  
  6. $obj  = new k();  
  7. ?>   


运行test.php报错:

Warning: require_once(k.php) [function.require-once]: failed to open stream: No such file or directory in F:\website\test.php on line 11

Fatal error
: require_once() [function.require]: Failed opening required 'k.php' (include_path='.;C:\php5\pear') in F:\website\test.php on line 11

恢复test.php代码

但是将j.php移到另外目录录入k下,

运行test.php报错:

Warning: require_once(j.php) [function.require-once]: failed to open stream: No such file or directory in F:\website\test.php on line 11

Fatal error
: require_once() [function.require]: Failed opening required 'j.php' (include_path='.;C:\php5\pear') in F:\website\test.php on line 11

这个时候是因为找不到j.php

所以需要修改test.php代码

  1. <?php  
  2.       
  3. function __autoload($class_name)   
  4. {  
  5.     require_once "k/".$class_name . '.php';  
  6. }  
  7. $obj  = new j();  
  8. ?>   

-----------------------------------------------------------------------------

为什么使用自动加载?
 

包含一般文件较少的情况会用手动包含要使用的类文件
当要包含大量类文件的时候,这样就会显得麻烦,就可以使用自动包含类。

类文件:test.php

class Test
{
   public function __construct()
   {
       echo __CLASS__.__FUNCTION__;
   }
}

 


 
1.手动包含:

require_once('test.php');
$test = new Test();

 

 


2.使用__autoload()自动包含:

// 这样实例化一个类的时候,将会自动包含同名的类文件
// 需要重载__autoload方法,自定义包含类文件的路径
function __autoload($classname)
{
    $class_file = strtolower($classname).".php";
    if (file_exists($class_file)){
        require_once($class_file);
    }
}
$test = new Test();

 

3.使用spl_autoload_register() 自定义的方法来加载文件
语法:bool  spl_autoload_register ( [callback $autoload_function] )

function myLoader($classname)
{
    $class_file = strtolower($classname).".php";
    if (file_exists($class_file)){
        require_once($class_file);
    }
}
// 注册自定义方法
spl_autoload_register("myLoader");

$test = new Test();


 

也可以使用类的方法来实现自定义的加载函数

class autoLoader
{
    public static function myLoader($classname)
    {
        $class_file = strtolower($classname).".php";
        if (file_exists($class_file)){
            require_once($class_file);
        }
    }
}

// 通过数组的形式传递类和方法,元素一为类名称、元素二为方法名称
// 方法为静态方法
spl_autoload_register(array("autoLoader","myLoader"));

$test = new Test();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值