You are here: Re: how do professional PHP developers handle defaults for function parameters? « PHP Programming Language « IT news, forums, messages
Re: how do professional PHP developers handle defaults for function parameters?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация