Make optionalparameters in your own defined class [ANSWERED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Make optionalparameters in your own defined class [ANSWERED]

Post by christian_phpbeginner »

Hi, I've seen some or more functions in PHP which have optional parameters. And this has made me wondered, could I define optional parameters in my own class ?

Thank you,
Chris
Last edited by christian_phpbeginner on Fri Aug 11, 2006 5:55 pm, edited 1 time in total.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

You need to specify a default value if you want it to be optional.

Code: Select all

function foo($required, $optional = null) { }
In that function you don't have to specify optional, but it will default to null.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

jamiel wrote:You need to specify a default value if you want it to be optional.

Code: Select all

function foo($required, $optional = null) { }
In that function you don't have to specify optional, but it will default to null.
hi jamiel...

Is it that simple ? Silly me !.....thanks for the reply, now I know how to declare an optional parameter inside a function in my own class.

Thanks a lot,
Chris
Post Reply