Variable argument count for constructors not permitted?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mavigozler

    Variable argument count for constructors not permitted?


    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.p hp':

    class MySQLDatabase {
    // properties here
    function __construct($se rverConnection, $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.

  • Janwillem Borleffs

    #2
    Re: Variable argument count for constructors not permitted?

    mavigozler schreef:
    So thus I have the following content in an 'include'd class file
    'MySQLDbClass.p hp':
    >
    class MySQLDatabase {
    // properties here
    function __construct($se rverConnection, $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($se rverConnection, $username, $password,
    $database = 'SOME_DEFAULT') {
    ....
    }


    JW


    Comment

    Working...