在php中
namespace ***; 为设定一个空间,以下的内容为这个空间的内容。
在不同的命名空间里,可以存在函数与const相同的名字,但是define的名字不能相同。
当访问相同名字的元素时,应该加上命名空间的访问方式,如:
namespace test1;
function t1(){
echo "test1";
}
namespace test2;
function t1(){
echo "test2";
}
\test1\t1();// 应该如此调用
tp5中,命名空间并不是真实存在的目录,而是巧妙的运用了这个目录
默认是放在公共空间,但是在某个特定空间里引入了有公共空间的函数,那么此时调用的是在这个空间的内容(引入的空间并不影响),
除非在当前空间找不到,才会到公共空间里去找。
1、非限定名称的访问方式:也就是没有限定访问空间,就是访问当前空间,也就是普通平时写的那样。
2、限定名称的访问方式:就是如test1\t1()这样的访问方式,是在目前的命名空间下进行访问。
3、完全限定名称的访问方式:就是如\test1\t1()这样的访问方式,可以跳出这个地方走到其他空间进行调用。
这个common是用来放一些重复使用率较高的函数,在到进入控制器之前这个common是已经被调用了的.
在所有的模块都可以使用。
创建某些高频率的使用函数有可能需要调用第三方类库,此时需要:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use think\Controller;
require '../extend/PHPMailer/src/Exception.php';
require '../extend/PHPMailer/src/PHPMailer.php';
require '../extend/PHPMailer/src/SMTP.php';
然后这个是封装好的一个phpmailer。
function SendEmail($receiver,$subject,$body){
try {
//Server settings
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->CharSet='UTF-8';
$mail->Host = 'smtp.sina.cn'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '13227855005m@sina.cn'; // SMTP username
$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
//Recipients
$mail->setFrom('13227855005m@sina.cn', 'wrm'); //后面这个是发送时候的昵称
$mail->addAddress($receiver,'Acmer'); //收件人的地址和对收件人的称号
// 这里是收件人的地址和收件人的昵称
//$mail->addAddress('收件人的地址'); // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'this body is wu,i juse test';
// return 0;
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
这里只传入了3个参数,使用的例子为
$receiver = 'w461184988@163.com';
$subject = 'I just test this email again too lalal ';
$body = 'hello,I am very happy when you see this information ';
SendEmail($receiver,$subject,$body);