Include file with his dependencies in PHP

My root project folder is:

c:\phpproject\data_valid.php

In this folder I have a library I need to use:

c:\phpproject\vendor\egulias\email-validator\src\EmailValidator.php

Inside data_valid.php I include the library I need:

//data_valid.php

include ('vendor\egulias\email-validator\src\EmailValidator.php');

The EmailValidator.php has dependencies:

//EmailValidator.php


namespace Egulias\EmailValidator;

use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;

When I run the data_valid.php script I get the error:

Fatal error: Uncaught Error: Class "Egulias\EmailValidator\EmailLexer" 

So, the data_valid.php file doesn't know where the EmailLexer.php is, being this file called from EmailValidation.php. If I include EmailLexer.php in data_valid.php it is found

 //data_valid.php
include ('vendor\egulias\email-validator\src\EmailValidator.php');
include ('vendor\egulias\email-validator\src\EmailLexer.php');

But more dependendencies are missing:

Fatal error: Uncaught Error: Class "Doctrine\Common\Lexer\AbstractLexer

It seems that PHP doesn't include the dependencies of a library automatically.

How do I do that?

PD. After some research I found that including:

include 'vendor/autoload.php';

It loads everything.

But I am still curious how to do it without using include 'vendor/autoload.php'. For example, I have a bunch of classes inside multiple files in a directory. In this scenario I need to call one class but that class depends in multiple classes defined in multiple files in that directory.

Answer

Solution:

Use composer: https://getcomposer.org/

It also allows for manual inclusion of custom classes etc.

I don't recommend manually including all the necessary classes as you would need some recursive search for this anyway.

Source