|
Posted by R. Rajesh Jeba Anbiah on 07/15/07 11:37
On Jul 7, 2:35 am, lawrence k <lkrub...@geocities.com> wrote:
> I'm working in PHP 4. Even if a parameter is mandatory, I nearly
> always give it a default value of "false". Then I print an error
> message if someone who is calling my function forgot the mandatory
> parameter:
>
> function outputHtmlToShowFile($fileName=false) {
> if ($fileName) {
> // code goes here
> } else {
> echo "In outputHtmlToShowAFile() the code expected the first
> parameter to be a file name, but instead the value was false";
> }
>
> }
>
> How do others handle default parameter values?
>
> I've been avoiding this:
>
> function outputHtmlToShowFile($fileName) { }
>
> because if I do this and someone forgets to pass a file name to the
> function, then the PHP parser issues an error. I've been assuming
> that relying on the PHP parser to write my error messages for me is
> unprofessional. Does anyone else feel that way?
IMHO, the default parameters options are for handling the
situation to have default values; it's not meant to professionally
handle exception of missing params.
If you define a function with default params in it, it would
usually mean that the function can be called without any params for
it's default behavior. So, if you want to handle missing params, use
exception/error handler.
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
Navigation:
[Reply to this message]
|