RE: How do YOU set default function/method params?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



IMO, array_merge() is easy to use, however it suppose to use more cpu than other options. If you are a performance freak, i suggest you to not choose array_merge() solution. (also you execute date() and md5() functions even if you wont need to use them)

In the other way, I think first option (with if() ones) seems more readable than other ones.

-----Original Message-----
From: Jim Lucas [mailto:lists@xxxxxxxxx]
Sent: Tuesday, October 06, 2009 4:21 AM
To: Eddie Drapkin
Cc: php General List
Subject: Re: [PHP] How do YOU set default function/method params?

Eddie Drapkin wrote:
> On Mon, Oct 5, 2009 at 8:48 PM, Jim Lucas <lists@xxxxxxxxx> wrote:
>> Here 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 ran
>>        #out of width in this email...
>>
>>    # do something here...
>>    mail(...);
>>
>> }
>>
>> sendEmail('john@xxxxxxx',
>>    'marykate@xxxxxxxx',
>>    '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
>>
>>
>
> How 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...
>
>       $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(...);
>
> }
>

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 Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



  ________________________________
Bu mesaj ve ekleri, mesajda gönderildiği belirtilen kişi/kişilere özeldir ve gizlidir. Size yanlışlıkla ulaşmışsa lütfen gönderen kisiyi bilgilendiriniz ve mesajı sisteminizden siliniz. Mesaj ve eklerinin içeriği ile ilgili olarak şirketimizin herhangi bir hukuki sorumluluğu bulunmamaktadır. Şirketimiz mesajın ve bilgilerinin size değişikliğe uğrayarak veya geç ulaşmasından, bütünlüğünün ve gizliliğinin korunamamasından, virüs içermesinden ve bilgisayar sisteminize verebileceği herhangi bir zarardan sorumlu tutulamaz.

This message and attachments are confidential and intended for the individual(s) stated in this message. If you received this message in error, please immediately notify the sender and delete it from your system. Our company has no legal responsibility for the contents of the message and its attachments. Our company shall have no liability for any changes or late receiving, loss of integrity and confidentiality, viruses and any damages caused in anyway to your computer system.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux