Jump to content

Recommended Posts

Hallo, I have a strange problem with a freshly installed composer project that is not loading the dependencies as the documentation states it should.

I just made a new directory (www/site/test/), which has a index.php file, and I want to be able to use composer-installed dependencies from within the index.php file, inside the test folder; is this the wrong way to do it?

Do I need to place my project root somewhere else for it to work?

Dependencies are installed like this:

composer require phpmailer/phpmailer


For some strange reason, the autoloader is not loading the dependencies, and I simply get a class not found, without any explanation as to why it was not found. The thing is, I can get it to work by resorting to crazy hackery like: new PHPMailer\PHPMailer\PHPMailer()

But the documentation clearly states that I should be able to do new PHPMailer() - hope this makes sense.

This is what I have in my index.php:
 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// define('BASE_PATH', rtrim(preg_replace('#[/\\\\]{1}#', '/', realpath(dirname(__FILE__))), '/') . '/');


// require BASE_PATH . 'vendor/autoload.php';
require __DIR__ . '/vendor/autoload.php';

 

Edited by JacobSeated

You're reading from some old documentation. PHPMailer\PHPMailer\PHPMailer looks weird but it is correct: the first "PHPMailer" is the organization, the second is the project, and the third is the class itself.

https://github.com/PHPMailer/PHPMailer/blob/master/UPGRADING.md

  • Thanks 1
5 minutes ago, requinix said:

You're reading from some old documentation. PHPMailer\PHPMailer\PHPMailer looks weird but it is correct: the first "PHPMailer" is the organization, the second is the project, and the third is the class itself.

https://github.com/PHPMailer/PHPMailer/blob/master/UPGRADING.md

Thanks, this is much appreciated. It actually looks like you are right, and this is the default behavior. Maybe this is better documented here, at least for PHPMailer: https://packagist.org/packages/phpmailer/phpmailer 

Use statements also seem to work. It looks like the vendor/project part is what is listed in composer.json, in the "require" section. I wonder if there is a "standard" way to include this in my index.php file, so I do not have to write all those use statements?
I read somewhere else that the vendor/composer/autoload_namespaces.php is supposed to update an array automatically, but the array in my file is just empty. Not sure if that has something to do with it?

In any case, this is not much different than how I do things in vanilla PHP, so it does not bother me too much; except, I have to sometimes guess at the right framework/path, since it does not seem to be documented in some cases.

I can see that some other packages still mention writing new someName(), without clarifying the part about namespaces (vendor/project).

 

8 minutes ago, JacobSeated said:

Use statements also seem to work. It looks like the vendor/project part is what is listed in composer.json, in the "require" section. I wonder if there is a "standard" way to include this in my index.php file, so I do not have to write all those use statements?

It's easier when you contain all your mailing work in one single location instead of spreading calls to it throughout your application. It's perfectly acceptable to create your own mailing class that uses PHPMailer to do the work.

 

Quote

I read somewhere else that the vendor/composer/autoload_namespaces.php is supposed to update an array automatically, but the array in my file is just empty. Not sure if that has something to do with it?

Don't worry about those files.

 

Quote

I can see that some other packages still mention writing new someName(), without clarifying the part about namespaces (vendor/project).

It should be mentioned somewhere, but otherwise it's mostly implied.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.