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

From: Date: Mon, 19 Oct 2009 17:10:34 +0000
Subject: Re: How do YOU set default function/method params?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-299060@lists.php.net to get a copy of this message
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@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
You could always check these functions: http://www.php.net/manual/en/function.func-get-arg.php http://www.php.net/manual/en/function.func-get-args.php http://www.php.net/manual/en/function.func-num-args.php They come with PHP 5.3. You can use them like Perl's shift if you like. PS Didn't bother to read the whole thread as you will all understand. -- Thodoris

« previous php.general (#299060) next »