Connecting Tech Pros Worldwide Help | Site Map

Variable argument count for constructors not permitted?

  #1  
Old November 17th, 2008, 07:05 AM
mavigozler
Guest
 
Posts: n/a

Is there a strict requirement for having the same number of parameters in
a call to a function and/or class method---in this case a class
constructor---and the definition of the function and/or class method?


BACKGROUND:

I am developing a MySQL-specific "database manager" class and my
constructor has 4 parameters: the first 3 parameters are those typically
used in the mysql_connect() call which the constructor calls to make the
connection, and an "optional" 4th argument which is a string that names
the database to be opened.

So thus I have the following content in an 'include'd class file
'MySQLDbClass.php':

class MySQLDatabase {
// properties here
function __construct($serverConnection, $username, $password,
$database) {
// argument checking code, including count of arguments
// code here

}
// more methods
}

And I instantiate the class:

$mySQLdbObj = new MySQLDatabase($serverSite, $uname, $password);

I get a PHP warning (and stopped execution) about incorrect use of the
number of arguments.

  #2  
Old November 17th, 2008, 08:55 AM
Janwillem Borleffs
Guest
 
Posts: n/a

re: Variable argument count for constructors not permitted?


mavigozler schreef:
Quote:
So thus I have the following content in an 'include'd class file
'MySQLDbClass.php':
>
class MySQLDatabase {
// properties here
function __construct($serverConnection, $username, $password,
$database) {
// argument checking code, including count of arguments
// code here
>
}
// more methods
}
>
And I instantiate the class:
>
$mySQLdbObj = new MySQLDatabase($serverSite, $uname, $password);
>
I get a PHP warning (and stopped execution) about incorrect use of the
number of arguments.
>
If you want to have an optional argument, just define the constructor as
follows:

function __construct($serverConnection, $username, $password,
$database = 'SOME_DEFAULT') {
....
}


JW


Closed Thread