Page 1 of 1

[SOLVED] Simple question about Swift_Address

Posted: Tue Jul 10, 2007 5:48 am
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!

Posted: Tue Jul 10, 2007 7:19 am
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 ;)

Posted: Tue Jul 10, 2007 6:21 pm
by GryphonLeon
Thanks a lot, that answered my question perfectly :)
I hope this helps someone else as well.