在 PHP (Laravel) 中更改单例邮件程序是否会影响其他用户请求?
Does changing a singleton mailer in PHP (Laravel) affect other user requests?
Laravel 5.3 Mailer class 根据本文配置为单例https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel
让我们假设一个用户在请求中设置 Mail::setSwiftMailer 到不同的服务器,以便通过他的 mailserver/account.
发送邮件
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
这会影响其他用户吗?据我了解,通常的单例特征不适用于 PHP http://blog.gordon-oheim.biz/2011-01-17-Why-Singletons-have-no-use-in-PHP/
A Singleton created in one Request lives for exactly that request. A
Singleton created in another Request done at the same time will still
be a completely different instance. And it will occupy it’s own
memory. Those instances are not linked to each other. They are
completely isolated, because PHP is a Shared-Nothing architecture. You
do not have one single unique instance, but many similar instances in
parallel processes.
那么改变单例功能的邮件服务器是否会改变其他用户的邮件服务器?
更改 Laravel 配置会影响其他用户吗?我假设至少这里的答案是肯定的。
Config::set('mail', ['driver' => $mail->driver, 'host' => $mail->host, 'port' => $mail->port, ...]);
在传统的 php 中,每个请求都是一个不同的实例。从一个实例到另一个实例(从一个请求到另一个)没有继承(变量方面)。
在同一个实例中,邮件程序在其生命周期内是一个单例
看看最简单的单例实现:
class Singleton {
protected static $_instance = null;
public static function newInstance()
{
if (null === self::$_instance)
{
self::$_instance = new self;
}
return self::$_instance;
}
// disallowed
protected function __clone() {}
// disallowed
protected function __construct() {}
}
它确保在这个单一请求中只存在一个这样的实例。
所以你的 Swift_SmtpTransport 是单身人士:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
这意味着 Swift_SmtpTransport 的每个实例都是您刚刚创建的实例(对于此请求)(这并不意味着您不能更改属性- 也许实例有 getters 和 setters 在创建后被调用。
现在让我们看看您已经发现了什么:
A Singleton created in one Request lives for exactly that request.
所以它是每个请求并且该请求也不会影响其他请求。所以 不,我不认为其他用户会受到影响。即使您根据单个请求 更改配置也不行。
有关更多信息,另请阅读:Simultaneous Requests to PHP Script - 对于每个请求,都会分叉一个新的独立进程,该进程与其他进程没有任何关系。唯一就是可能会互相挡住。
Laravel 的文档说:
Configuration values that are set at run-time are only set for the
current request, and will not be carried over to subsequent requests.
Laravel 5.3 Mailer class 根据本文配置为单例https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel
让我们假设一个用户在请求中设置 Mail::setSwiftMailer 到不同的服务器,以便通过他的 mailserver/account.
发送邮件$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
这会影响其他用户吗?据我了解,通常的单例特征不适用于 PHP http://blog.gordon-oheim.biz/2011-01-17-Why-Singletons-have-no-use-in-PHP/
A Singleton created in one Request lives for exactly that request. A Singleton created in another Request done at the same time will still be a completely different instance. And it will occupy it’s own memory. Those instances are not linked to each other. They are completely isolated, because PHP is a Shared-Nothing architecture. You do not have one single unique instance, but many similar instances in parallel processes.
那么改变单例功能的邮件服务器是否会改变其他用户的邮件服务器?
更改 Laravel 配置会影响其他用户吗?我假设至少这里的答案是肯定的。
Config::set('mail', ['driver' => $mail->driver, 'host' => $mail->host, 'port' => $mail->port, ...]);
在传统的 php 中,每个请求都是一个不同的实例。从一个实例到另一个实例(从一个请求到另一个)没有继承(变量方面)。
在同一个实例中,邮件程序在其生命周期内是一个单例
看看最简单的单例实现:
class Singleton {
protected static $_instance = null;
public static function newInstance()
{
if (null === self::$_instance)
{
self::$_instance = new self;
}
return self::$_instance;
}
// disallowed
protected function __clone() {}
// disallowed
protected function __construct() {}
}
它确保在这个单一请求中只存在一个这样的实例。
所以你的 Swift_SmtpTransport 是单身人士:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
这意味着 Swift_SmtpTransport 的每个实例都是您刚刚创建的实例(对于此请求)(这并不意味着您不能更改属性- 也许实例有 getters 和 setters 在创建后被调用。
现在让我们看看您已经发现了什么:
A Singleton created in one Request lives for exactly that request.
所以它是每个请求并且该请求也不会影响其他请求。所以 不,我不认为其他用户会受到影响。即使您根据单个请求 更改配置也不行。
有关更多信息,另请阅读:Simultaneous Requests to PHP Script - 对于每个请求,都会分叉一个新的独立进程,该进程与其他进程没有任何关系。唯一就是可能会互相挡住。
Laravel 的文档说:
Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.