Search code examples
phpincludeset-include-path

Including files in PHP scripts from any directory with absolute path


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.


Solution

  • 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