The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
Consider the following directory structure −
A directory called "master", that has two files named 'worker_1', and 'worker_2'. The master directory itself is a subfolder of the main project directory.
The project directory also contains an index.php file.
Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies −
project_directory ├── master │ ├── worker_1.php │ └── worker_2.php └── index.php
If we execute the code −
include "master/worker_1.php";
from the index.php, it runs successfully.
But to run worker_1.php by including worker_2.php, a relative include to the index.php file has to be done, as shown below −
include "master/worker_2.php";
Using __DIR__ will get this running. From worker_1.php the below code can be executed −
<?php include __DIR__ . "/worker_2.php";