Question

Possible Duplicate:
PHP require file from top directory

I've been using PHP for a long time but I've always just written it to get the job done, now I'm trying to learn best practices and improve my skill. Right now I'm wondering what is the best method for including files in PHP scripts from any directory.

I have always done the following:

<?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';
?>

But if my files are in a subdirectory I need to do something like:

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

Is there a better way to be doing this? I'm starting to learn OOP so if there is an OOP solution that applies I'm all ears.

Thank you.

Était-ce utile?

La solution 2

did you try autoloading? http://php.net/manual/en/language.oop5.autoload.php .. Sorry I dont have permissions to write comments yet..

Updated.. Wesley is right, you will need to have the class name same as file name for autoload to work. I am sorry to assume that you are doing OOP-PHP. If not autoload will not work you will have to do it traditional way

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

Any file inside the ABSPATH with classname = filename will be automatically loaded.

If you have various paths with different files in them than you will have to create multiple constant variables with path name. And than call them inside autoload with the specific path.

for eg.

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

and if you are not using OOP. Than you will have to define different paths as constants and include the files the way you do it now. and if you want to automate this process

this code will be helpful.

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

this will include all the files present in the folder ABSPATH. You can create a function and call it with whatever path you want to put.

Hope this helps.

Dins

Autres conseils

I usually have something similar to this in my application/Bootstrap.php (for a zend framework application).

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

Some of those get fed to an autoloader, others I use for direct include paths like require_once MODEL_PATH . 'Directory/Directory/IncludeFile.php';

when I include something I use the dirname(__FILE__) to get the path to the file that try to include.

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

But if you use OOP I suggest you to look at some class loader that will include the file automaticaly for you when you use a class. There is some already coded that exist like the one with Doctrine and the one with Composer.

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

http://getcomposer.org/

 set_include_path('.'.PATH_SEPARATOR.'your path to includes folder'
            . PATH_SEPARATOR . './library/'
);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top