[SOLVED] Simple question about Swift_Address

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
GryphonLeon
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 5:38 am

[SOLVED] Simple question about Swift_Address

Post by GryphonLeon »

Hi,

I have a simple question that can be solved in seconds probably :)

I've created a function to create a connection and send an email (text/html/mime depending on it's parameters).
The function has a number of parameters, two of them being $to and $from, like so: function send_email($to,$from,......)

The $to and $from variables can either be a string (email address) or an array with two fields, email and name.

Then, in the function, I did the following:

.... connection stuff etc....
$recipient = (is_array($to)) ? new Swift_Address($to['email'],$to['name']) : $to;
$sender = (is_array($from)) ? new Swift_Address($from['email'],$from['name']) : $from;
$mail_sent = $mailer->send($message,$recipient,$sender);

I'm using PHP4 and everything works perfectly. My question however is: should I use "$recipient = new Swift_Address()" or "$recipient =& new Swift_Address()" ? Both work fine, but I'm wondering which one I should use. Is it =& for PHP4 and = for PHP5?

PS, I'm aware I should switch to an if/else structure if I have to use the =&.

Thanks a lot in advance!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It doesn't really matter in this particular case, but generally you should always use =&. Unless you're trying to deliberately create copies (aka Value Objects) then it's a good idea to always use the reference operator in PHP4.

The reason it works fine in this case is because the object is only read from rather than written to so all copies that get made will have the same values anyway ;)
GryphonLeon
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 5:38 am

Post by GryphonLeon »

Thanks a lot, that answered my question perfectly :)
I hope this helps someone else as well.
Post Reply