1

可能重复:
PHP 需要来自顶层目录的文件

我已经使用 PHP 很长时间了,但我一直只是为了完成工作而编写它,现在我正在努力学习最佳实践并提高我的技能。现在我想知道从任何目录将文件包含在 PHP 脚本中的最佳方法是什么。

我一直在做以下事情:

<?php
    // From settings.php
    define('ABSPATH', '/home/dalyn/public_html/');

    // Then I put this at he top of all of my other files

    include 'includes/settings.php';
    include ABSPATH . 'includes/db.php';
    include ABSPATH . 'includes/functions.php';
?>

但是,如果我的文件位于子目录中,我需要执行以下操作:

<?php
    include '../includes/settings.php';
    include ABSPATH . 'includes/db.php';
    include ABSPATH . 'includes/functions.php';
?>

有没有更好的方法来做到这一点?我开始学习 OOP,所以如果有适用的 OOP 解决方案,我会全力以赴。

谢谢你。

4

4 回答 4

2

我的 application/Bootstrap.php (用于 zend 框架应用程序)中通常有类似的东西。

define( 'BASE_PATH'          , dirname( __DIR__ ) . '/'                          ); //!< application root directory: note this file is not in the root directory  
define( 'VAR_PATH'           , BASE_PATH        . 'var/'                         ); //!< optional system generated config files
define( 'CACHE_PATH'         , BASE_PATH        . 'cache/'                       ); //!< caches in the filesystem. See ISV_CacheManager
define( 'APPLICATION_PATH'   , BASE_PATH        . 'application/'                 ); //!< where the modules live
define( 'MODEL_PATH'         , APPLICATION_PATH . 'models/'                      ); //!< path to models
define( 'FORM_PATH'          , APPLICATION_PATH . 'forms/'                       ); //!< path to forms
define( 'CONTROLLER_PATH'    , APPLICATION_PATH . 'modules/default/controllers/' ); //!< path to default module controllers
define( 'HELPER_PATH'        , APPLICATION_PATH . 'helpers/'                     ); //!< global zend view helpers
define( 'INCLUDE_PATH'       , APPLICATION_PATH . 'include/'                     ); //!< useful support classes
define( 'DOCUMENT_ROOT'      , $_SERVER['DOCUMENT_ROOT']                         ); //!< http root directory. note _no_ trailing slash.  
define( 'IMAGE_PATH'         , '/image/'                                         ); //!< from the http document root 
define( 'SCRIPT_PATH'        , '/js/'                                            ); //!< from the http DOCUMENT_ROOT

其中一些被送入自动加载器,其他一些我用于直接包含路径,如 require_once MODEL_PATH 。'目录/目录/IncludeFile.php';

于 2012-11-10T00:35:52.070 回答
1

你试过自动加载吗?http://php.net/manual/en/language.oop5.autoload.php ..对不起,我还没有写评论的权限..

更新了.. Wesley 是对的,您需要将类名与文件名相同才能使自动加载工作。我很抱歉假设您正在做 OOP-PHP。如果不是自动加载将不起作用,您将不得不以传统方式进行

function __autoload($class) {
require ABSPATH. $class .".php";
}

ABSPATH 中任何带有 classname = filename 的文件都将被自动加载。

如果您有各种路径,其中包含不同的文件,那么您将必须创建多个具有路径名的常量变量。而不是使用特定路径在自动加载中调用它们。

例如。

class Myprogram{

public function __construct(){

define('ABSPATH', '/home/dalyn/public_html/');
define('ABSPATH_1', '/home/dalyn/public_html/includes');
define('ABSPATH_2', '/home/dalyn/public_html/some_other_folder');

}

function __autoload($class) {
require ABSPATH. $class .".php";
    require ABSPATH_1. $class .".php";
    require ABSPATH_2. $class .".php";
 }   

}

//Some other file
$myProg = new Myprogram(); // this will define your constants as well as autoload all the required files

如果您不使用 OOP。比您必须将不同的路径定义为常量并以您现在的方式包含文件。如果你想自动化这个过程

这段代码会很有帮助。

if ($handle = opendir(ABSPATH)) {
  while (false !== ($entry = readdir($handle))) {
     if ($entry != "." && $entry != "..") {
        include_once ABSPATH . $entry;
    }
  }
  closedir($handle);
}

这将包括文件夹 ABSPATH 中存在的所有文件。您可以创建一个函数并使用您想要放置的任何路径调用它。

希望这可以帮助。

丁斯

于 2012-11-09T23:51:57.007 回答
0
 set_include_path('.'.PATH_SEPARATOR.'your path to includes folder'
            . PATH_SEPARATOR . './library/'
);
于 2012-11-10T03:14:54.083 回答
0

当我包含某些内容时,我使用dirname(__FILE__)来获取尝试包含的文件的路径。

<?php
include dirname(__FILE__).'/includes/db.php';

但是,如果您使用 OOP,我建议您查看一些在您使用类时会自动为您包含文件的类加载器。有一些已经编码存在,例如使用 Doctrine 的编码和使用 Composer 编码的编码。

http://www.doctrine-project.org/

http://getcomposer.org/

于 2012-11-09T23:55:34.117 回答