2

I'm working on a simple class right now to get my head around OOP and I need some help with a function. The function receives various parameters, some of are optional:

public function Test($req, $alsoreq, $notreq = null, $notreq2 = null, $notreq3 = null)
{ 
    ...
}

How do I call the function, passing the two first parameters and the last one?

For example:

Test('req', 'alsoreq', 'notreq3'); 

Ignoring the notreq2 and notreq?

I tried

Test('req', 'alsoreq', '', '', 'notreq3');

but this seems ugly and hackish.

hakre
  • 193,403
  • 52
  • 435
  • 836
Vinny
  • 77
  • 3
  • 7
  • 2
    Think carefully about whether you actually want all these parameters. Some of them may be better as member variables, which you could set in the constructor or with setter methods. – Skilldrick Oct 02 '10 at 15:37
  • I'm building a MySQL query "interface". It will allow me to make selects more easily. For example: query('table', '*', '', '', '0,10'); Being: query($table, $from, $where, $orderby, $limit) – Vinny Oct 02 '10 at 15:49
  • Some related questions: [Passing named parameters to a php function through call_user_func_array](http://stackoverflow.com/q/6610556/367456); [PHP Optional Parameters - specify parameter value by name?](http://stackoverflow.com/q/4029404/367456); [PHP Function Argument to an array](http://stackoverflow.com/q/7603517/367456) – hakre Jun 15 '12 at 09:40

3 Answers3

9

You can't. Think about it, if you could do that, how would PHP be able to tell if the third argument is supposed to be the $notreq, $notreq2 or $notreq3? A less hackish way would be:

Test($req, $alsoreq, NULL, NULL, $notreq3);

As you are making it clear that the first two optional args are NULL.

Choose the orders of arguments wisely, so the ones that get used more often come first.
Alternatively, you can use an array:

public function Test($req, $alsoreq, array $notreq) { ... }

// no optional args
Test($req, $alsoreq);

$optional = array('arg1' => 'something', 'arg2' => 'something else');
Test($req, $alsoreq, $optional);

$optional = array('arg3' => 'hello');
Test($req, $alsoreq, $optional);
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • Was going to submit my answer, but I refreshed the page just to check if you edited yours. Alas, you have ;) +1 – BoltClock Oct 02 '10 at 15:39
  • If you're going to do this, you might as well have Test take only one parameter — an array. – Paul Schreiber Oct 02 '10 at 15:40
  • I see... thanks for the tip. Unfortunately, i guess i'll stick to what i was doing, since it's much simpler to treat inside the class. – Vinny Oct 02 '10 at 15:40
  • Do you guys know if this is somehow wrong accordingly to the Zend PHP Best Practices? – Vinny Oct 02 '10 at 15:41
  • @Vinny, what's considered wrong practice is somewhat of a moot point with PHP, seeing as the language itself isn't very consistent. I used to struggle with such questions, but after working with other languages, I've let go of my guilt and just accepted that, I'm the one working around PHP's limitations. In Python for example you can do this with ease. But PHP doesn't embrace a specific way, some people code as in Java, others as in Perl and others yet as in Python. I suspect the pythonists would be more incline to adopt the implementation showcased in this answer. – Michael Ekoka Oct 02 '10 at 16:16
1

There seem to be no way. As written in php manual:

Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.

You are trying to put non-default value (notreq3) on the right of default (notreq2, notreq1). Ugly and hackish way is likely to be more correct.

Regards, Serge

zserge
  • 2,212
  • 2
  • 31
  • 40
0
  1. Write a function called aboutMe.
  2. It takes two parameters $name and $age.
  3. It should echo out "Hello! My name is $name, and I am $age years old.".
  4. Call your function and check out the results panel to see if it works!

Hint

"Hello! My name is " . $name . " and I am " . $age . " years old."
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213