1

I'm doing a lot of refactoring on a project so it can actually have development branch. Right now, all includes are hard-coded in and it's really messy. A lot of includes will point to the same file, but the includes look very different from each file (and will, by necessity, have to be different for a dev site, also some copying of identical files to different directories for the time being).

I've looked at this answer: StackOverflow: Path of current PHP script relative to document root and tried to implement a

getpath.php with

<?php
define("DIR_BASE", dirname( __FILE__)."/");
define("SERV_CONFIGS", DIR_BASE."server/specific/config.php");

to give me a decent starting spot that can adapt to overall location (as well as a few environment-specific location variables), so that way I could just change every instance of

include ("../../../include/foo.bar");
include ("http://example.com/something/else.php");
include ("../../../server/specific/config.php");

and

include ("../include/foo.bar");
include ("../something/else.php");
include ("http://server/specific/config.php");

and

include ("http://example.com/include/foo.bar");
include ("/something/else.php");
include ("/server/specific/config.php");

etc. all into just

include (DIR_BASE . "include/foo.bar");
include (DIR_BASE . "something/else.php");
include (SERV_CONFIGS);

That way, I can just copy-paste those two lines through the hundreds of files and be done. (it's going to be much more than just three file names).

However, it dawned on me that each page will STILL need to be a bit of "unique snowflake" to include my getpath.php, each one pointing from that individual file to getpath.php to include it.

How can I include getpath.php without making each page have its own snowflake version of include("getpath.php")?

Community
  • 1
  • 1
lilHar
  • 1,735
  • 3
  • 21
  • 35
  • Why do you need the full `http://...` path for some files? – Dan Dec 04 '15 at 23:33
  • [Set the PHP include path using htaccess](http://stackoverflow.com/questions/6508429/how-to-set-the-php-include-path-via-htaccess) – Dan Dec 04 '15 at 23:34
  • 2
    If application is objective, you can read about autoloading in PHP. – Kamil P Dec 04 '15 at 23:34
  • @dan08 I don't, at least not to my knowledge... but a lot of the legacy code I inherited I'm having to go in and change has that. – lilHar Dec 04 '15 at 23:36
  • @dan08 using htaccess methods isn't an option for me at the moment. I wish it was (and I'll remember it for later). Our current htaccess files are extremely messy, and I've noticed minor changes can have profound and unexpected results on the rest of the site in ways that don't even begin to make sense to me at present, and it's going to take many weeks to untangle *that* mess, let alone start adding new functionality to it. – lilHar Dec 04 '15 at 23:39
  • This sounds like a super fun project ;). Autoloading is probably worth the 'barrier to entry' that will undoubtedly come with it. – Dan Dec 04 '15 at 23:41
  • @dan08 Superfun. :P Entire codebase was previously written by a guy just out of school, as I understand who just up and left one day. Apparantly they had a bunch of other developers turn down the job despite good pay. They took one look at the code-base and went "uh... no." I'm the first they got to say yes, and I mainly said yes because the previous two codebases I worked on were even worse (One was still using php3. :P ) That said, I'm a little fuzzy on autoloading. Any good references? – lilHar Dec 04 '15 at 23:47

1 Answers1

1

You can use $_SERVER['DOCUMENT_ROOT'] in any file that needs to include getpath.php:

include $_SERVER['DOCUMENT_ROOT'] . '/path/to/getpath.php';

This avoids writing specific paths for differents files like:

include '../../../../../getpath.php';
CJ Nimes
  • 648
  • 7
  • 10
  • I was wondering why someone down-voted this answer? (I normally leave comments on why whenever I do so others can learn why it was downvoted.) – lilHar Dec 04 '15 at 23:41