4

I'm having trouble with nested includes. While I have seen there are a number if similar issues, they don't seem to help.

Generally I don't have problems with includes, but recently I have been trying something new and I cannot get the nested includes to work.

One solution: php nested include behavior

Basic setup:

  • index.php includes '/include/header.php'
  • header.php includes '/resources/login/index_alt.php'
  • index_alt.php includes '/resources/login/index_auth.php'
  • index_auth.php include '/class/Login.class.php' and '/class/Connection.class/php'

I do not actually write the paths like this (its to understand the depth). This is how it will look on the pages.

index.php:

  • include('include/header.php');

header.php: (header is included at every depth level except for /resources/...)

  • include('../resources/login/index_alt.php');

index_alt.php:

  • include('index_auth.php');

index_auth.php:

  • include('../../class/Login.class.php');
  • include('../../class/Connection.class.php');

At some depth levels the header file is accepted, but includes nested in will not...

3
  • 1
    I'd advise you rethink your including strategy, including files which themselves include files which in turn may also include file can only lead to trouble in the long run as the dependencies become increasingly muddled. You're better off doing all the includes in once place, namely the script that is being accessed directly. Even better, if you're using an object-oriented approach, you could use an autoloader. Commented Jul 12, 2012 at 18:41
  • Look into include_once() or require_once(). This way you dont have to worry about including a single file multiple times. Commented Jul 12, 2012 at 18:46
  • @GordonM thanks for your comment. it isn't quite as bad as you think. with this extra feature, I require code that has already been implemented elsewhere. Commented Jul 12, 2012 at 19:25

2 Answers 2

4

assuming that the filesystem looks like this..

/www
   include/header.php
   class/Login.class.php
   class/Connection.class.php
   resources/login/index_alt.php
   resources/login/index_auth.php
   index.php

this means that

index.php: include(__DIR__ . '/include/header.php');
header.php: include(__DIR__ . '/../resources/login/index_alt.php');
index_alt.php:  include(__DIR__ . '/index_auth.php');

etc; see http://php.net/manual/en/language.constants.predefined.php

Sign up to request clarification or add additional context in comments.

Comments

2

Instead of traversing up the directory tree with ../ use dirname(__FILE__). Also, you probably want include_once() or require_once() to avoid other potential problems:

index.php:

include('include/header.php');

header.php:

include(dirname(dirname(__FILE__)) . '/resources/login/index_alt.php');

(Notice that dirname(__FILE__) will return the current directory, but dirname(dirname(__FILE__)) will return the parent)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.