最近,我检查了SwiftMailer包,发现这个问题我无法解释:
// #1
$message = new Swift_Message($subject)->setFrom($f)->setTo($t)->setBody($body);
// #2
$message = new Swift_Message($subject);
$message->setFrom($f)->setTo($t)->setBody($body);
// #3
$message = new Swift_Message($subject);
$message->setFrom($f);
$message->setTo($t);
$message->setBody($body);
变体#1来自SwiftMailer文档,不起作用,它提供了一个“意外的parse >‘>”解析错误.这个问题很容易解决,变体#2和#3很好用。
我认为方法链接是PHP中广泛使用的一种技术,我还认为#1是完全有效的。为什么它不能像预期的那样工作呢?
我的PHP是V7.1.1
阿明。
发布于 2017-09-22 02:10:44
示例一在文档中不是这样写的,类实例化的示例方法链中没有一个是有效的PHP。
相反,文档是这样编写的:
// Create the message
$message = (new Swift_Message())
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(['john@doe.com' => 'John Doe'])
// Set the To addresses with an associative array (setTo/setCc/setBcc)
->setTo(['receiver@domain.org', 'other@domain.org' => 'A name'])
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf'));
注意,类是在括号内实例化的。这允许直接从构造函数链接方法。
https://stackoverflow.com/questions/46361706
复制