kefoseki
kefoseki

Reputation: 71

PHP how to include composer autoload in Class file

I got difficult to include composer autoload in Class file, it not working on require_once('../vendor/autoload.php');

require_once('phpmailer/PHPMailerAutoload.php');
require_once('../vendor/autoload.php');

class Test {

    function X()
    { ... }

}

What is the proper way to load multiple include files in a class?

Upvotes: 5

Views: 12065

Answers (2)

Matteo
Matteo

Reputation: 39380

If you (correctly) use composer you need do add only the vendor autoload file. Then add the other dependency via composer vendor library or add custom path (composer do the rest for you).

As example, more simply:

  1. start in an empty directory
  2. launch the command:

php composer.phar init

  1. Add the dependency of the library in the composer.json files (if you don't add it in the init process) with the command (suggested by the packagist site)

composer require phpmailer/phpmailer

  1. Then your class should be like:

    require_once('../vendor/autoload.php');
    
    class Test {
    
    function X()
    { ... }
    
    }
    

Hope this help

Upvotes: 7

vlad awtsu
vlad awtsu

Reputation: 195

I think you want something like this

class Loader
{
    public function __construct()
    {
        require_once('phpmailer/PHPMailerAutoload.php');
        require_once('../vendor/autoload.php');
    }
}

$loader = new Loader();

just add some function as you want

tell me if this help you ... goodluck

Upvotes: -3

Related Questions