Re: How do YOU set default function/method params?
| From: | Jim Lucas | Date: | Tue, 06 Oct 2009 01:20:41 +0000 |
| Subject: | Re: How do YOU set default function/method params? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298624@lists.php.net to get a copy of this message | ||
Eddie Drapkin wrote:
On Mon, Oct 5, 2009 at 8:48 PM, Jim Lucas <lists@cmsws.com> wrote:Good, since it is a combination of the examples I gave. I am looking at how you would solve the problem. Unless this is the way you would solve the problem.. :-D Jim -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William ShakespeareHere is a problem that I have had for years now. I have been trying to come up with the perfect solution for this problem. But, I have come down to two different methods for solving it. Here is the problem... <?php function sendEmail( $to, $from, $subject, $body, $attachments=array(), $headers=array() ) { # I typically do not put each argument on seperate lines, but I ranHow does this look to you? function sendEmail( $to, $from, $subject, $body, $attachments=array(), $headers=array() ) { # I typically do not put each argument on seperate lines, but I ran#out of width in this email...# do something here... mail(...); } sendEmail('john@doe.com', 'marykate@uhhh.net', 'Hi!', 'Check out my new pictures!!!', $hash_array_of_pictures ); Now, we all have a function or method like this floating around somewhere. My question is, how do YOU go about setting the required entries of the $headers array() ? I see three possible solutions. I want to see a clean and simple solution. Here are my ideas so far: function sendEmail( $to, $from, $subject, $body, $attachments=array(), $headers=array() ) { # I typically do not put each argument on seperate lines, but I ran#out of width in this email...if ( empty($headers['Date']) ) {$headers['Date'] = date('c');} if ( empty($headers['Message-ID']) ) {$headers['Date'] = md5($to.$subject);} # and the example goes on... # do something here... mail(...); } Or, another example. (I will keep it to the guts of the solution now)$headers['Date'] = empty($headers['Date']) ? date('c') : $headers['Date'];$headers['Message-ID'] = empty($headers['Message-ID']) ?md5($to.$subject) : $headers['Message-ID'];OR, yet another example... $defaults = array('Date' => date('c'),'Message-ID' => md5($to.$subject), ); $headers += $defaults; END of examples... Now, IMO, the last one is the simplest one and for me, I think it will be the new way that I solve this type of problem. But, my question that I put out to all of you is...How would you solve this problem?TIA Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php#out of width in this email...$default_headers = array( 'Date' => date('c'), 'Message-ID' => md5($to.$subject) ); $headers = array_merge($default_headers, $headers); # and the example goes on... # do something here... mail(...); }