Hi all, I'm still playing around with OOP and to be totally honest really enjoying it! I can't believe I haven't made the switch from procedural earlier...

Something I would like to know is, how can I set default arguments for methods / functions?

What i'm getting at is, if I write this: $user->getFirstname() it should automatically se that there are no arguments and therefore use the value of $_SESSION['userid'] as the argument for that method. However if I write $user->getFirstname("10") it should see the argument and use 10 as the userid instead for that particular call on the method.

I'm currently reading through http://php.net/manual/en/functions.arguments.php and I think the answer is there; it's just my lack of understanding as I'm looking at the examples cluelessly.


Edited by mmcdonald

3 Years
Discussion Span
Last Post by mmcdonald

I have a hunch that when I write the method I declare the argument like so:

public function getFirstname($userid = $_SESSION['userid']){

then if I leave the argument empty: $user->getFirstname(); it will automatically use the userid session value. However if I write $user->getFirstname("10") it will use 10 for the userid? Is that right?


Or am I being a twat... should I use if(isset($userid)){//code} within the actual method... that would make sense wouldn't it... yes Michael it would.


Haha :). You are right in your second post.

function doSomething($value1, $value2 = 'test')
    echo $value1 . $value2;

// Will output "hellotest"

// Will output "hellohello"
doSomething('hello', 'hello');

However, keep in mind that it is RECOMMENDED to place predefined parameters at the end of your function. I believe you will get a notice in PHP strict if you don't. E.g.:

Don't: function doSomething($value1 = 'abc', $value2, $value3)
Do: function doSomething($value1, $value2, $value3 = 'abc')

Edited by minitauros

Votes + Comments
Helpful information :D Thank you!

Cheers fella, glad I was on the right lines haha +1

Another quick query, if I use $userid as an argument in lots of methods (50+) then is there a viable method of using a 'globalish' variable to set the argument?

Let me clarify...

If I'm going to use the $userid example above identically in lots of methods is there a way to write it once and be done with it for all methods in the class or should I just pass the argument settings in all of the methods individually?

Edited by mmcdonald


if I use $userid as an argument in lots of methods (50+) then is there a viable method of using a 'globalish' variable to set the argument?

Set it as a property. If the whole class is about the user, then this is valid practice.

Not sure how your class looks, but using $_SESSION inside it is not recommended. It's much better to pass it as a parameter if you need it.

Edited by pritaeas

Votes + Comments
Don't you just love 'to the point' responses :D Thanks
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.