5

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?

Matteo
  • 37,680
  • 11
  • 100
  • 115
kefoseki
  • 71
  • 2
  • 8
  • Your asking an OOP way to to require files right ? – vlad awtsu Jun 16 '17 at 03:14
  • If you're new, please read and learn. Good place to start is to read the [Composer Basic Usage](https://getcomposer.org/doc/01-basic-usage.md) guide. – josephting Jun 16 '17 at 03:15
  • @vladawtsu, yes, I am studying something like `spl_autoload` – kefoseki Jun 16 '17 at 03:17
  • The whole purpose of using the [autoloading](http://php.net/manual/en/language.oop5.autoload.php) is to remove the need of manually including all the needed files. All you have to do is to include `vendor/autoload.php` once, in the script that provides the entry point of your application. – axiac Jun 16 '17 at 07:44
  • Can't tell from your code, but usually you have to run the composer file once in the command line aka terminal when you install a framework. – NathanC Jun 16 '17 at 03:00

2 Answers2

7

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

Matteo
  • 37,680
  • 11
  • 100
  • 115
-3

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

vlad awtsu
  • 185
  • 1
  • 2
  • 14