Variable argument count for constructors not permitted?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


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

   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.

Re: Variable argument count for constructors not permitted?

mavigozler schreef:
Quoted text here. Click to load it

If you want to have an optional argument, just define the constructor as

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


Site Timeline