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 6340193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:47:21+00:00 2026-05-24T19:47:21+00:00

My previous question broght up some discussion which suggested that I am wrong to

  • 0

My previous question broght up some discussion which suggested that I am wrong to use the likes of

require_once($_SERVER['DOCUMENT_ROOT'] . '/proj/database/database-common.php');

and should, instead be using

require_once(dirname(__FILE__) . '/database/database-common.php');

BUT, I have a multi-level hierarchy and some common functionality (dataebase, error handling, etc) which can be called from any level – how do I handlt that? E.g, my error handling requires a CSS file from $_SERVER[‘DOCUMENT_ROOT’]\css\my.css

If I do not have $_SERVER['DOCUMENT_ROOT'] as an anchor point, can I use something else? If not, how do I handle that a common file may wish to include another, but does not know from which level it itself is being included?

E.g.:

root
     `- databse
      `- validation

If validation/common.php wants to include database/database_common.php thhen, without an anchor, validation/common.php cannot know the path to database_common.php It is not ..\database/database_common.php if validation/common.php was incldued from a file in the root directory (and this is a simplified example)

My philiosophy is to always require a file in the file which uses it, so I can’t just include everthing in the root directory files ….

Can someoen clarify best pracise on this for me? Thanks

  • 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-05-24T19:47:21+00:00Added an answer on May 24, 2026 at 7:47 pm

    You could use a bootstrap file which is used across all others file and define all global parameters there, something like

    # bootstrap.php
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__)));
    

    and the require this master file in every file which needs to include other files so your following requires will become

    require 'path/to/bootstrap.php';
    require APPLICATION_PATH . '/database/common.php';
    

    This will solve in part your problem, but keeps thing consistent, the only part which change is the first require, but you can use relative paths to keeps code portable.

    I’d not use include_path changes because it will slow path resolution because php will search through all directories in the include path and stat is one of the most expensive syscall. Example: if you you have this include path
    .:/dir1:/dir2:/dir3 and you require a file named foo.php which is in dir3 php will make the following four stat calls:

    ./foo.php
    /dir1/foo.php
    /dir2/foo.php
    /dir3/foo.php
    

    For this reason you should always use absolute paths when requiring files and should keep the include path limited to one or two paths.

    Alternatively, if you’re using OOP you can always use autoloading for your classes which remove the need for requiring files and works pretty well. A good example of this technique is given in Zend Framework autoloader

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

Sidebar

Related Questions

My previous question concluded that a distasteful double cast might be necessary to use
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
New question As suggested by Asaph in previous question: Regex to check if exact
This question is in continuation to my previous question, in which I asked about
On Previous Question , someone recommended that I install Andy's IDE Fix Pack to
My previous question about this subject was answered and I got some tests working
In previous question I got comment about Oracle statistics: Oracle doesn't know that 50M
This question follows on from a previous question which can be found here: Need

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.