2017-05-17 40 views
0

如何自动加载可能存储在多个文件夹内的多个文件? 什么,我已经不喜欢被证实在知情同意: enter image description here使用php自动加载多个文件夹中的多个文件?

创建在index.php文件[的init.php]所需的文件,其中包含:

<?php 
spl_autoload_register(function ($class){ 
    require_once "classes/class.$class.php"; 
}); 

问题没有1:如何自动加载另一个文件夹,例如:Conf/class.Conf.php问题编号2:我可以使用其他自动加载过程的另一个名称约定吗?

它会更好,如果你提供一个编码例子:)

+1

使用PSR-4自动装载机(它与作曲家一起) –

+0

@tereško你可以举个例子吗? –

+0

http://www.php-fig.org/psr/psr-4/examples/ –

回答

0

解决的问题如@丹 - 米勒评论提到检查文件是否存在问题,然后要求它为每一个新的文件夹

<?php 
spl_autoload_register(function ($class){ 
    $filename="classes/$class.php"; 
     if(!file_exists($filename)) 
     { 
      return "file : $filename is not Exist on the Given Path"; 
     } 
    require_once "classes/$class.php"; 
}); 
spl_autoload_register(function ($class){ 
    $filename="conf/$class.php"; 
     if(!file_exists($filename)) 
     { 
      return "file : $filename is not Exist on the Given Path"; 
     } 
    require_once "conf/$class.php"; 
}); 

* **这是测试目的