argument order
Moderator: General Moderators
argument order
Are there any documents which discuss the order in which arguments should be sent to functions/methods in?
Any of you have any preferences, best practices?
Any of you have any preferences, best practices?
I find that only being able to have ending arguments as options is a weak area in PHP.
For example..
causes a [s]fatal[/s] parse error.
There are reasons behind my opinion on this, and the __construct not being able to "decide" what object it wants to return. Not anything I really want to get into at this point.
I don't think adding this functionality would take someone more than an hour or two.
For example..
Code: Select all
function arguments($a = 1, $b = 2, $c = 3, $d = 4)
{
echo "a= $a<br/>";
echo "b= $b<br/>";
echo "c= $c<br/>";
echo "d= $d<br/>";
}
arguments(,2,3,4);There are reasons behind my opinion on this, and the __construct not being able to "decide" what object it wants to return. Not anything I really want to get into at this point.
I don't think adding this functionality would take someone more than an hour or two.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
As long as PHP is a typeless language, if they were to add overloading, it would be flawed and based purely on the amount of arguments. The language would have to add set data types before that implementation would even make sense. Until then, we've already got overloading based on arguments, just not in the traditional sense.astions wrote:I don't think adding this functionality would take someone more than an hour or two.
And in you're example, what'd you'd want (PHP-wise) is:
Code: Select all
function arguments($a = NULL, $b = 2, $c = 3, $d = 4)
{
if($a == NULL) a = 1;
echo "a= $a<br/>";
echo "b= $b<br/>";
echo "c= $c<br/>";
echo "d= $d<br/>";
}
arguments(NULL,2,3,4);I don't understand what your saying or what the problem is. The way I see it is if you set a default value for an argument that is optional, and you don't pass that argument through when you call the function, it should set it to the default value.
PHP is saying that there is a parse error because there is a comma without an argument before it. This is a missing argument and should be treated as one because the function clearly indicates the argument is optional.
Passing through NULL is the same as passing through a value... a NULL value.
PHP is saying that there is a parse error because there is a comma without an argument before it. This is a missing argument and should be treated as one because the function clearly indicates the argument is optional.
Passing through NULL is the same as passing through a value... a NULL value.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Adding "elisions" ( ,, ) would require slightly more work because there must be a way for the engine to distinguish between "null" or "empty" and "skipped" arguments.astions wrote:
I don't think adding this functionality would take someone more than an hour or two.
Much more useful would be ability to handle key arguments, like:
Code: Select all
function printPersonalData($name, $age, $state) {
....
}
#call
printPersonalData('name' => 'joe', 'state' => 'NY, 'age' => 30);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I don't know if I could agree it's all that useful. Certainly at times it could be, but I'd rather have overloading.stereofrog wrote:Much more useful would be ability to handle key arguments, like:
Code: Select all
function printPersonalData($name, $age, $state) { .... } #call printPersonalData('name' => 'joe', 'state' => 'NY, 'age' => 30);
- kyberfabrikken
- Forum Commoner
- Posts: 84
- Joined: Tue Jul 20, 2004 10:27 am
That would be nice indeed. Until that happens, it's still possible to use an associative array, to achieve about the same thing.stereofrog wrote: Much more useful would be ability to handle key arguments, like:
Code: Select all
function printPersonalData($name, $age, $state) { .... } #call printPersonalData('name' => 'joe', 'state' => 'NY', 'age' => 30);
Code: Select all
function printPersonalData($args) {
$name = $args['name'];
$age = $args['age'];
$state = $args['state'];
....
}
printPersonalData(Array('name' => 'joe', 'state' => 'NY', 'age' => 30));Some other things that have annoyed me is that you can't do things like..
And you also can't do this..
Code: Select all
class blah
{
private $var = 'x';
function some_method($x = $this->var)
{
}
}Code: Select all
function some_function($a = array())
{
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
This can't be done in any language that I know.. nor would I want it.astions wrote:Code: Select all
class blah { private $var = 'x'; function some_method($x = $this->var) { } }
Considering it's not scalar, i.e. requires evaluation... yeah.astions wrote:Code: Select all
function some_function($a = array()) { }
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
I could have sworn I've seen this done before...astions wrote:Code: Select all
function some_function($a = array()) { }
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
No OO language would ever let you do things like
$this->x is not a fixed value so the method signature could be different every time you call it. That would very quickly create a nice mess. The proper thing to do is explicitly evaluate at runtime.
In fact, until you're inside the curly braces you're not in scope of $this anyway...
EDIT | I should point out that when we are referring to constant values it will work fine:
Code: Select all
function foo($x = $this->x)
{
}Code: Select all
function foo($x=null)
{
if ($x === null) $x = $this->x;
//
}EDIT | I should point out that when we are referring to constant values it will work fine:
Code: Select all
public function foo($x = self::SOME_CONSTANT)
{
//
}