0

So maybe I'm not getting how PHP includes work. I have a file structure like this:

app--|
     |
     -classes-|
              -PHPMyClass.php
     |
     -includes-|
               -PHPMyInclude.php
     |
     -functions-|
                -TestFunctions-|
                               -PHPCallTestOnMyClass.php

so in PHPMyClass.php I do this:

<?php
    require_once '../includes/PHPMyInclude.php';

and if I test my PHPMyClass.php directly it works fine, e.g., it properly locates PHPMyInclude.php

however, if I attempt to include PHPMyClass.php in my PHPCallTestOnMyClass.php script, the load will fail since it can't find '../includes/PHPMyInclude.php' since it appears to be trying to reference this from the root of app/functions/testfunctions, which of course will not resolve properly. How do I manage locations in php layouts? I've resorted to taking the includes out of the sub files and hardcoding the includes into the PHPCallTestOnMyClass.php code, e.g.,

<?php
    require_once '../../includes/PHPMyInclude.php';
    require_once '../../classes/PHPMyClass.php';

but this seems unnecessarily complicated and unsupportable. What am I missing?

I'm also running into this same problem when trying to use phpunit. There has to be something I'm just goofing up.

3 Answers 3

2

You should use the full system path to the file to avoid this issue:

include("/path/from/root/to/inc/example.php"); 

A common thing to do is define a variable or constant that defines the root path to your web files. That way if it ever changes (i.e. you change hosts) you only need to change it in one place.

In your config file:

define('ROOT_PATH', '/path/from/root/to/');

In your PHP files;

include(ROOT_PATH . "inc/example.php"); 
Sign up to request clarification or add additional context in comments.

7 Comments

I don't think it should be necessary to explicitly have to state the path to a file, and it isn't.
@Maniaque It's not necessary, but it's smart.
why is it smart? Why should I want to include the entire system file location in my includes? What would happen if it was moved to a server that wasn't laid out exactly as the original. Wouldn't that mean I'd have to edit every include statement?
No, because if you did it smartly (like I did above) it would be defined in one place only. Then you can move to a new server and only have to change your config file which you almost certainly would have to do anyway.
by "smartly" I'm assuming you're referring to the ROOT_PATH environment variable? Well yes, this definitely a better solution, but aren't you essentially reinventing the _DIR_ variable?
|
0

PHP relative paths are not relative to the files's location but the called file's location.
So, if you have an index.php file:

<?
include('includes/db.php');

An includes/db.php:

<?
include('user.php');

An user.php file:

<?
echo('root level');

An includes/user.php file:

<?
echo('includes folder');

If you access your index.php you'll see root level, since the include from includes/db.php is being called relatively to index.php.

1 Comment

Yes I understand this... which is why I adjusted my code to use the _DIR_ "magic" variable.
0

My solution of choice was to use the __dir__ Magic constant as described here.
This allows me to use reference pathing as I would expect it...
For example:
require_once (__DIR__ . "/../enums/WorkUnitType.php");

will find the WorkUnitTypes.php file in reference of the directory of the php file it's being required from. This is exactly what I wanted.

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.