I'm learning php oop, going through books etc and having a go at my own.
In my __construct I have a number of parameters that have default values (in fact all of them do). However when I try to write code to create a new object I'm having syntax error problems.
For example my construct statement has 6 parameters. I passed values to the first three, wanted to skip the next two and set a value for the last one. I simply put commas with nothing between them but it throws up a syntax error.
What should I put in place of nothing to accept the default value?
Thanks
Pass NULL instead of nothing
Example:
$obj = new Whatever($parm1,$parm2,$parm3,NULL,NULL,NULL);
or in your constructor set the to null
__construct($parm1 = NULL,$parm2 = NULL,$parm3 = NULL,$parm4 = NULL,$parm5 = NULL,$parm6 = NULL)
And call it like this
$obj = new Whatever();
UPDATE from your comment:
Your code:
function __construct(
$userid,
$magicCookie,
$accessLvl = 'public',
$det = 'basic',
$sortOrder = 'starttime',
$recurring = 'true',
$d_from = "01/01/2011",
$d_to = "31/01/2011",
$max = "10") {
// Code goes here...
}
So calling like this
// After the seconds parameter you have default values
$obj = new Whatever(
$userid,
$magicCookie
);
What do you think about this?
function __construct(
$userid,
$magicCookie,
$accessLvl = NULL,
$det = NULL,
$sortOrder = NULL,
$recurring = FALSE,
$d_from = NULL,
$d_to = NULL,
$max = NULL) {
// Code goes here... Yes set Defaults if NULL
}
Call it like this:
$obj = new Whatever(
$userid,
$magicCookie,
$accessLvl = 'public',
$det = 'basic',
$sortOrder = 'starttime',
$recurring = TRUE,
$d_from = "01/01/2011",
$d_to = "31/01/2011",
$max = 10);