Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Mandatory function arguments:

    function foo($arg1,$arg2) { return; }
    foo('bar');

    Warning: Missing argument 2 for foo()
Lame. But omitting arg2 is allowed if you give it a default value:

    function foo($arg1,$arg2=FALSE) { return; }
Blah. Instead I'd rather make arg2 truly optional just by testing if it's defined.


I see this as a feature. (I'd never thought I come around to defend PHP. Issue #21 has bitten me in practice.)


I really don't mind this, I have more more control because of it. If I want the function to error out upon arg omissions, I will demand arguments.

If I don't require args: function foo($arg1=null, $arg2=null) {return;} foo(); // return no error

This is great for me, when I am writing an API and have some args that are required and others that are optional.


What would the ideal language return for your function call foo('bar'). It would seem to me that missing an argument should return some sort of notice, like in the case where someone else is using it.


I prefer Javascript for example: JQuery's calling conventions would not be possible if function args were mandatory.


I'm curious as what PHP couldn't do? You have default argument values and you can pass more arguments to any function. Seems that covers all the cases.


You can always use func_get_args() if you truly want it that way.


Or you could just use a real programming language that has some kind of design.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: