0

What is the difference between these two in PHP?

  require "./vendor/autoload.php";

vs

  require "vendor/autoload.php";

For both statements the autoload.php script is found, but in certain environment the autoloader itself does not find classes. I'm not trying to solve the autoloader problem itself, but try to understand why these two make it behave differently.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43
  • 3
    Possible duplicate of [What does the dot-slash do to PHP include calls?](http://stackoverflow.com/questions/579374/what-does-the-dot-slash-do-to-php-include-calls) – j08691 Nov 07 '16 at 16:05
  • The second one starts searching for the file from the current dir, as the second one. But the second mothod is prefered as it was taken from linux/unix system. The dot strictly tells the system to search in the current dir. Which is why often linux softwares install/configure instructions start with `cd foo/ && ./configure` – samayo Nov 07 '16 at 16:05

2 Answers2

3

The . refers to the folder that you are in, it's most a unix syntax for files them for the php. I think you should use __DIR__ to prefix the included files, so you can avoid some problems with relative paths

malukenho
  • 151
  • 8
1

The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them).

Lets take the following structure:

/index.php
/file2.php
/folder/
       /file1.php

If index.php includes file1.php, and you want file1.php to include file2.php - you can do this using require './file2.php'; (inside file1.php, which is in the inner folder).

If you use require 'file2.php'; inside file1.php you are looking for file2.php inside the folder (which will give you an error, because the file is not there).

Dekel
  • 60,707
  • 10
  • 101
  • 129