Welcome! Log In Create A New Profile

Advanced

[PHP] PHP cannot use a provided class

Posted by James Moe 
James Moe
[PHP] PHP cannot use a provided class
March 20, 2018 09:40PM
Hello,
php v5.6.34

I am working on a very simple PHPMailer; all it has to do is send an
email. I am using the latest version from the Github repository, v6.0.3.
Running the program produces this:

Fatal error: Class 'PHPMailer' not found in
/home/sohne/web/php/common/php-test-email.php on line 44

The program obviously loads the class files; there is no error there.
Yet trying to create a new instance fails.

What am I missing?

----[ The start of the program ]----
define('PHP_COMMON', '/home/sohne/web/php/common/');

require(PHP_COMMON . 'phpmailer-603/PHPMailer.php');
require(PHP_COMMON . 'phpmailer-603/Exception.php');
require(PHP_COMMON . 'phpmailer-603/SMTP.php');

//Import the PHPMailer class into the global namespace
// use PHPMailer\PHPMailer\PHPMailer;

$from = '[email protected]';

$to = $sendto;
$replyto = '[email protected]';
$cc = '';
$bcc = '';

// use PHPMailer;

$mail = new PHPMailer();
**Splat!**
----[ end ]----


This is the head portion of the PHPMailer Class:

namespace PHPMailer\PHPMailer;
class PHPMailer
{
---- the rest of the class ---
}

--
James Moe
jmm-list at sohnen-moe dot com
Think.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: PHP cannot use a provided class
March 20, 2018 11:20PM
On 20.03.2018 at 21:37, James Moe wrote:

> php v5.6.34
>
> I am working on a very simple PHPMailer; all it has to do is send an
> email. I am using the latest version from the Github repository, v6.0.3.
> Running the program produces this:
>
> Fatal error: Class 'PHPMailer' not found in
> /home/sohne/web/php/common/php-test-email.php on line 44
>
> //Import the PHPMailer class into the global namespace
> // use PHPMailer\PHPMailer\PHPMailer;

Either properly import the class:

use PHPMailer\PHPMailer;

> $from = '[email protected]';
>
> $to = $sendto;
> $replyto = '[email protected]';
> $cc = '';
> $bcc = '';
>
> // use PHPMailer;
>
> $mail = new PHPMailer();

Or use the fully qualified class name:

$mail = new \PHPMailer\PHPMailer;

See also the chapter about namespaces in the PHP manual:
http://www.php.net/manual/en/language.namespaces.php

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
James Moe
[PHP] Re: PHP cannot use a provided class
March 21, 2018 06:10AM
On 03/20/2018 03:14 PM, Christoph M. Becker wrote:

>> //Import the PHPMailer class into the global namespace
>> // use PHPMailer\PHPMailer\PHPMailer;
> Either properly import the class:
>
> use PHPMailer\PHPMailer;
>
Using the above causes the script to silently abort. No messages, nothing.

>>
>> $mail = new PHPMailer();
> Or use the fully qualified class name:
>
> $mail = new \PHPMailer\PHPMailer;
>
This yields the message:

Fatal error: Class 'PHPMailer\PHPMailer' not found in
/home/sohne/web/php/common/php-test-email.php on line 44

>
> See also the chapter about namespaces in the PHP manual:
> http://www.php.net/manual/en/language.namespaces.php
>
Apparently php.net is having issues. The page persistently times out.

--
James Moe
jmm-list at sohnen-moe dot com
Think.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: PHP cannot use a provided class
March 21, 2018 11:00AM
On 21.03.2018 at 05:58, James Moe wrote:

> On 03/20/2018 03:14 PM, Christoph M. Becker wrote:
>
>>> //Import the PHPMailer class into the global namespace
>>> // use PHPMailer\PHPMailer\PHPMailer;
>> Either properly import the class:
>>
>> use PHPMailer\PHPMailer;
>>
> Using the above causes the script to silently abort. No messages, nothing.
>
>>>
>>> $mail = new PHPMailer();
>> Or use the fully qualified class name:
>>
>> $mail = new \PHPMailer\PHPMailer;
>>
> This yields the message:
>
> Fatal error: Class 'PHPMailer\PHPMailer' not found in
> /home/sohne/web/php/common/php-test-email.php on line 44

Sorry, forget that. Actually, the class PHPMailer is contained in the
PHPMailer\PHPMailer namespace, so you need the triple

$mail = new \PHPMailer\PHPMailer\PHPMailer;

>> See also the chapter about namespaces in the PHP manual:
>> http://www.php.net/manual/en/language.namespaces.php
>>
> Apparently php.net is having issues. The page persistently times out.

Hmm, works for me. The issue might be intermittent, or maybe there is
something wrong with the geolocation and/or a certain mirror. Try:

http://us2.php.net/manual/en/language.namespaces.php
https://secure.php.net/manual/en/language.namespaces.php

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Iliffe
Re: [PHP] Re: PHP cannot use a provided class
March 21, 2018 05:40PM
On Wednesday 21 March 2018 00:58:59 James Moe wrote:
> On 03/20/2018 03:14 PM, Christoph M. Becker wrote:
> >> //Import the PHPMailer class into the global namespace
> >> // use PHPMailer\PHPMailer\PHPMailer;
> >
> > Either properly import the class:
> > use PHPMailer\PHPMailer;
>
> Using the above causes the script to silently abort. No messages,
> nothing.
>
> >> $mail = new PHPMailer();
> >
> > Or use the fully qualified class name:
> > $mail = new \PHPMailer\PHPMailer;
>
> This yields the message:
>
> Fatal error: Class 'PHPMailer\PHPMailer' not found in
> /home/sohne/web/php/common/php-test-email.php on line 44
>
> > See also the chapter about namespaces in the PHP manual:
> > http://www.php.net/manual/en/language.namespaces.php
>
> Apparently php.net is having issues. The page persistently times out.

I'm not an expert on PHP by any means but one thing that seems odd to me is
the mixing of Windows format and Linux format path separators. Should it
be "\PHPMailer\PHPMailer" or "/PHPMailer/PHPMailer" ? The error messages
seem to suggest Linux ('/').

John

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] Re: PHP cannot use a provided class
March 21, 2018 07:10PM
On March 21, 2018 4:22:10 PM UTC, John Iliffe <[email protected]> wrote:
>On Wednesday 21 March 2018 00:58:59 James Moe wrote:
>> On 03/20/2018 03:14 PM, Christoph M. Becker wrote:
>> >> //Import the PHPMailer class into the global namespace
>> >> // use PHPMailer\PHPMailer\PHPMailer;
>> >
>> > Either properly import the class:
>> > use PHPMailer\PHPMailer;
>>
>> Using the above causes the script to silently abort. No messages,
>> nothing.
>>
>> >> $mail = new PHPMailer();
>> >
>> > Or use the fully qualified class name:
>> > $mail = new \PHPMailer\PHPMailer;
>>
>> This yields the message:
>>
>> Fatal error: Class 'PHPMailer\PHPMailer' not found in
>> /home/sohne/web/php/common/php-test-email.php on line 44
>>
>> > See also the chapter about namespaces in the PHP manual:
>> > http://www.php.net/manual/en/language.namespaces.php
>>
>> Apparently php.net is having issues. The page persistently times
>out.
>
>I'm not an expert on PHP by any means but one thing that seems odd to
>me is
>the mixing of Windows format and Linux format path separators. Should
>it
>be "\PHPMailer\PHPMailer" or "/PHPMailer/PHPMailer" ? The error
>messages
>seem to suggest Linux ('/').
>
>John

No, namespaces in php use the backslash, they're not the same as path separators, as path location and namespace don't actually have to be linked (unless you're following psr standards)
Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
James Moe
[PHP] Re: [SOLVED] PHP cannot use a provided class
March 21, 2018 07:40PM
On 03/21/2018 02:54 AM, Christoph M. Becker wrote:

>> Fatal error: Class 'PHPMailer\PHPMailer' not found in
>> /home/sohne/web/php/common/php-test-email.php on line 44
> Sorry, forget that. Actually, the class PHPMailer is contained in the
> PHPMailer\PHPMailer namespace, so you need the triple
>
> $mail = new \PHPMailer\PHPMailer\PHPMailer;
>
Bingo! Yes, that allowed the script to run.
Thank you.

--
James Moe
jmm-list at sohnen-moe dot com
Think.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login