Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:32:06+00:00 2026-06-14T02:32:06+00:00

Possible Duplicate: PHP require file from top directory I’ve been using PHP for a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:32:08+00:00Added an answer on June 14, 2026 at 2:32 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: including php file from another server with php i want to have
Possible Duplicate: Escaping <? on php shorthand enabled server when using require What I
Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: PHPMailer AddAddress() Here is my code. require('class.phpmailer.php'); $mail = new PHPMailer(); $email
Possible Duplicate: php mail() function on localhost I am using php mail(). I have
Possible Duplicate: error using include_once with a php variable My code is like this:
Possible Duplicate: How do I call PHP parent methods from within an inherited method?
Possible Duplicate: PHP - send file to user I have written a script that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.