php nested include behavior

In many places in my code, I do things like:

file1.php: <?php include('../file2.php'); file2.php: <?php include('anotherdirectory/file3.php');

Depending on the server or settings I try this on, it either sets the relative paths from the "includer" or from the "includee". This is really confusing. So file1 might try to include "../anotherdirectory/file3.php" or it might try "anotherdirectory/file3.php".

What settings dictate this behavior? I want to have control over this...

-------------Problems Reply------------

In cases when I need to use relative paths I use the following syntax:

include (realpath(dirname(__FILE__)."/another_folder/myfile.php"));

I would recommend using absolute paths. A good way to do this while still being portable is to make a declaration like this in your public_html/index.php:

define('ROOT', dirname(__FILE__));

Then, you can write includes like this which are very easy:

include(ROOT.'/file.php');

Otherwise, PHP checks to see if the file is in the include path as defined by your php.ini. If it's not there, it tries a relative path to the current script. Which is unpredictable and unmaintainable since you may be nestingly including files from different relative locations.

Edit: If you're constantly including a lot of class files, you may want to look into autoloading. It makes everything way simpler if you're programming in an object-oriented style. I have personally never written the word 'include' in my code for a very long time.

Edit 2: You could use the php.ini directive auto_prepend_file to automatically include a one-line file with the definition of ROOT to each one of your scripts.

As someone on the php learning curve, I have found the best way to reference include paths is by absolute location, not relative, by using the built-in $_SERVER superglobal. In my own files I have been using this with success:

include $_SERVER [ 'DOCUMENT_ROOT' ] . '/path_from_root/file_name.php';

This way it doesn't matter where the included file resides relative to my calling file, and I don't have to worry about manually typing in my fully qualified server path. (Maybe obvious..) This will work no matter how nested the include call is, and if / when I move the calling file to a different directory, for example.

You can use this method with include, require, and any other file-related functions that need a path.

On a related note..

$_SERVER [ 'PHP_SELF' ]

will return the path (relative to the root) of the current file. I also use this quite a bit.

$_SERVER has other useful info you may want to check out here:

http://php.net/manual/en/reserved.variables.server.php

Sorry if this is an older thread, I'm new here.

EDIT: You could save this 'DOCUMENT_ROOT' to a variable for use later, but from recent experience I would recommend against it because then you have to worry about variable scope. The include line as written will work every time regardless of current scope.

With get_include_path() you can see, what the server configuration for this is. In most cases it looks like this:

.:/usr/lib/php

This means, the first place php is looking for a included file is the directory of the script that includes another. If it is not present there, php is looking in /usr/php/lib. If you add more paths, php will also look there for a matching file.

If you include a file, which includes another one, the "root" path is the path of the file which included another one at first.

Category:php Views:0 Time:2010-05-18

Related post

  • Activerecord Nested :include fails 2010-01-14

    I have an AR query using 'will_paginate' that looks like this: paginate :all, :page => criteria[:page], :per_page => criteria[:per_page], :include => { :user, :person }, :conditions => [conditions , criteria[:from_date], criteria[:to_date

  • Achieving NHibernate Nested Transactions Behavior 2010-04-19

    I'm trying to achieve some kind of nested transaction behavior using NHibernate's transaction control and FlushMode options, but things got a little bit confusing after too much reading, so any confirmation about the facts I list below will be very u

  • nested include in php 2010-06-11

    The directory structure: C:/wamp/www/application/model/data_access/data_object.php C:/wamp/www/application/model/users/user.class.php C:/wamp/www/application/controller/projects.php C:/wamp/www/application/controller/links/links.php I have 2 php file

  • Problem with nested include in selenium 2010-11-05

    I've noticed a problem for nested include function. When I use include in already included html file, selenium ide doesn't hide line with include command. Re-execution of scpits doubles number of include calls. As a result of test all the lines with

  • Are there any downsides from using Include() or nested include's()? 2011-11-17

    I am not an expert in PHP. I am trying to increase the use of include() to make my website code as clean as possible instead of just copying, for example, code of the header in all the pages. I have two questions 1 . Is it good practice to use includ

  • Nested Includes in Ruby on Rails 3 2012-02-25

    In Ruby on Rails I have School which has many children. Children which has many activities. And Activity which has one activity_types. I need help nesting includes. In my Children Controller. I have this... which works. s = School.find(params[:school

  • Simulating has_and_belongs_to_many nested through behavior in Rails 3 2010-07-30

    So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought

  • RoR nested :include to include sub-resources in to_xml/to_json 2010-12-14

    I have a weird data model situation to start with, so maybe my whole approach is wrong. Here's what I'm doing: I have a class called Bird and a simple class called Color. Conceptually, each bird has two to_many associations to Color, one for male col

  • C++ - Nested include - Avoiding 'include nested too deeply error' 2011-05-16

    What is the best way of declaring my header files if I want to have the following connections in my C++ code, just so that I don't get the 'include nested too deeply error'? On my edge class, I have some functions that need to return a Node object. S

  • Why does ui:Include behavior vary with different bean scopes? 2011-06-24

    I have been tinkering around with ui:include lately and have found it to be fairly quirky in many of its behaviors. After a some testing, it seems to me like these quirks have to do with issues go back to how ui:include fits into the jsf lifecycle. I

  • Nesting included Twig templates? 2011-10-10

    I'd like to pass the output of an included Twig template to another included Twig template as a parameter, like so: {% include 'MyBundle:Default:tpl1.html.twig' with {'item': include 'MyBundle:Default:tpl2.html.twig'} %} Unfortunately, this does not

  • Reduce multiple nested includes for the same table into a single query? 2011-12-13

    I am trying to reduce my SQL queries in a controller action and have the following query @event = Event.includes( :outgoing_orders => [:organisation], :outgoing_invoices => [:organisation] ).find(params[:id]) This works well, but I wanted to kn

  • Prevent including the same file during nested include 2012-01-03

    Assume the following files: a.php b.php c.php a.php includes c.php b.php includes c.php a.php includes b.php This makes a.php include c.php twice. Is there any way to remove this conflict? --------------Solutions------------- The include_once() state

  • Customizing multi-model nested form behavior 2012-05-01

    I am following the Rails guide for doing multi-model nested form. I have 2 models, Page and Picture. Page has_many Pictures. I'm putting the Picture file field within the Edit Page form using fields_for. Each time I upload an image, the form will add

  • ActiveRecord Nested Includes producing SQL Error 2010-01-13

    paginate :per_page => per_page, :page => page, :include => [{:stores => :locations}, :categories, :kosher], :origin => origin, :conditions => ["products.name LIKE ? #{conditions}", "%#{search}%"], :within => distance, :order =

  • PHP variable scope with nested include returned by function 2010-08-14

    Thanks in advance for any help. :) Ok, here's my problem. Simplified version of the code: global space with include file include file where $var is defined and function is called that returns an include statement include file returned by function and

  • Nested `include()` directives - How to include a file (which includes another) from more than one directories? 2011-05-31

    I have these three files to work with: document_root/include/config.php document_root/include/database.php document_root/index.php And the relevant part of the file contents are like below: config.php // ... $MyVar = 100; // ... database.php // ... r

  • Can I rely on nested #includes? 2011-06-22

    Possible Duplicate: C++ style question: what to #include? When I #include a header file, and I also need other files that are already #included from the first one, should I rely on the first #include or should I #include all of them? I know that it w

  • PHP nested include problem 2011-08-08

    This is engine.php (works fine): <?php include ('settings/server_settings.php'); include ('settings/tables.php'); ?> Now i create settings.php in the same folder, and with the same content as engine.php, and then i include this file to engine.p

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.079 (s). 11 q(s)