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

tereško
  • 58,060
  • 25
  • 98
  • 150
Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97
  • 1
    Stop doing include-oriented programming – tereško Sep 13 '17 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.... – Timothy Coetzee Sep 13 '17 at 16:35
  • Next step is doing OOP, because then you can use autoloaders. – tereško Sep 13 '17 at 16:53

2 Answers2

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?

Script47
  • 14,230
  • 4
  • 45
  • 66
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';
erwan
  • 887
  • 8
  • 17
  • 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...? – Timothy Coetzee Sep 13 '17 at 16:38
  • 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) – erwan Sep 13 '17 at 16:45
  • DOnt exactly understand your explanation except that define() is global but thanks anyway – Timothy Coetzee Sep 13 '17 at 16:49