1

Quick Rookie Question. Ive searched SO for answer but what I found wasnt really clear or addressed my particular problem as you can see here, and here.

Im trying to add a script in a different directory however this will result in all my require() paths to be wrong.

How can I move files around / change directories without having to modify my require() statements, if possible at all?

I believe __DIR__ is what Im looking for as pointed out in this question, however in linked question the answer used __FILE__ instead of __DIR__ , which I dont quite understand..?

Example

Consider this, my current working directory

enter image description here

require_once '../config/connection.php'; require_once '../views/head.php'; require_once'../classes/register.php';

: :MORE CODE

If I move the require statments to the scripts directory, as can be seen in image, it will result in not found error.

Is there anyway how I can avoid this except for appending another 2 ../ to each require() statements.

Hope my question makes sense...? Is it possible to do what Im trying to achieve?

Thanx

3
  • 1
    Stop doing include-oriented programming Commented Sep 13, 2017 at 16:23
  • @tereško Thank you for your comment. Im learning PHP, backend programming, its hard enough to grasp the basic concepts already, as my application gets larger I MUST use include() statements. Once I have the hang of things I will move on to OOP and MVC.... Commented Sep 13, 2017 at 16:35
  • Next step is doing OOP, because then you can use autoloaders. Commented Sep 13, 2017 at 16:53

2 Answers 2

1

If you are using spl_autoload_register then within that you can specify different locations to search for the classes,

spl_autoload_register(function ($class) {
    if (file_exists('some/location/' . $class . '.php')) {
          // Require!
    } else if (file_exists('some/other/location/' . $class . '.php')) {
         // Require!
    }
});

This way you can create backward compatibility and gracefully change your structure until you are sure that no files exist in a location then a condition can be removed from the autoloader.

The reason I never used __autoload:

Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.

Or you could have a file at the root of your DIR and use a define,

define('ROOT', __DIR__);

Then call it whenever you need to access files,

ROOT/path/to/location/from/root.php

Reading Material

Is there any difference between __DIR__ and dirname(__FILE__) in PHP?

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

5 Comments

Wouldn't the PHP include_path be the simpler way to solve this problem? Auto-loading on demand seems like overkill.
@alexis I was updating the post to provide multiple solutions that way to OP can decide what is best for him.
But you still haven't gotten around to mentioning include_path!
What include path?
Ah, that explains it. See my comments under the question. Much more flexible than autoloading classes, since it's not limited to classes or OO programming!
1

that could help

 <?php

    define('ROOT', '/absolute/path/to/your/root/folder/');

    require_once ROOT . 'config/connection.php';
    require_once ROOT . 'views/head.php';
    require_once ROOT . 'classes/register.php';

3 Comments

Thanks/ Questions: Is define() global? Should I add define('ROOT', '/absolute/path/to/your/root/folder/'); to the root and then use the suggested examples...?
yes it is, you can include it in your root file and go along with the example. but then you can't access to those file directly without passing throught the one who call the define method (hope im clear)
DOnt exactly understand your explanation except that define() is global but thanks anyway

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.