argument order

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

argument order

Post by Benjamin »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The only rule is optional arguments go after required ones. Generally speaking, the more frequently used arguments should come first.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I find that only being able to have ending arguments as options is a weak area in PHP.

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);
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering that's not how most other languages work, it probably wouldn't go over well.

If you want something to choose what object to return you need a Factory.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:I don't think adding this functionality would take someone more than an hour or two.
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.

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);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Right, but that's only if you MUST have that variable first. Like feyd said, the optional variables should come last, and the most used ones first.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

astions wrote:
I don't think adding this functionality would take someone more than an hour or two.
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.

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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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);
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. :)
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

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);
That would be nice indeed. Until that happens, it's still possible to use an associative array, to achieve about the same thing.

Code: Select all

function printPersonalData($args) {
  $name = $args['name'];
  $age = $args['age'];
  $state = $args['state'];
   ....
}

printPersonalData(Array('name' => 'joe', 'state' => 'NY', 'age' => 30));
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Some other things that have annoyed me is that you can't do things like..

Code: Select all

class blah
{
    private $var = 'x';

    function some_method($x = $this->var)
    {

    }
}
And you also can't do this..

Code: Select all

function some_function($a = array())
{

}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

astions wrote:

Code: Select all

class blah
{
    private $var = 'x';

    function some_method($x = $this->var)
    {

    }
}
This can't be done in any language that I know.. nor would I want it.
astions wrote:

Code: Select all

function some_function($a = array())
{

}
Considering it's not scalar, i.e. requires evaluation... yeah.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:

Code: Select all

function some_function($a = array())
{

}
I could have sworn I've seen this done before...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Probably in the function call, and not in the actual function.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No OO language would ever let you do things like

Code: Select all

function foo($x = $this->x)
{

}
$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.

Code: Select all

function foo($x=null)
{
    if ($x === null) $x = $this->x;
    //
}
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

public function foo($x = self::SOME_CONSTANT)
{
  //
}
Post Reply