Re: Custom function

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

 



On Wed, Feb 16, 2011 at 3:21 PM, Peter Lind <peter.e.lind@xxxxxxxxx> wrote:

> On 16 February 2011 21:00, Dan Schaefer <dan@xxxxxxxxxxxxxxxxxxxx> wrote:
> > In my code, I set the optional parameter to NULL and check for triple
> equals
> > "===" or "!==" to see if the variable has been passed with a value. IMO,
> > this is the safest way.
> >
> > function MyFunction($x, $y, $z=NULL) {
> > if ($z !== NULL) {
> > // Do Something
> > }
> > }
>
> In case you're actually trying to test if a variable was passed or not
> that doesn't work (as it doesn't detect NULL passed in). Use
> func_get_args() as that gives you any and all arguments passed to the
> function, excluding defaults.
>

I'm not sure what you're saying here, Peter? Are you saying that the code
wouldn't detect if $z was set to NULL by the calling code or by the default?
I believe the point would be that no matter the case, the check would still
perform the same task.

Maybe I misunderstood (sorry.)


>
> That said, if you're making use of optional parameters and need to
> check if anything was passed in, you're almost certainly doing things
> wrong.
>

I sometimes use this approach. PHP doesn't to my knowledge allow you to use
function calls within defaults. There are times that I want the default to
be the result of a function, and to accomplish this, I'll often set the
default to null, then check for the null and carry out the function within.

Is there a better solution?

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

[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