For example, when $db_password and $db_name will be missing, an error like this will be printed:
Some information is missing (db_password, db_name)
Here is how I did it at first - this is the beginning of my constructor:
Code: Select all
public function __construct($db_host = null, $db_username = null, $db_password = null, $db_name = null)
{
/* First, we make sure that all the required settings exist */
/* Create an array from the arguments supplied to the constructor */
$db_info_array = compact('db_host', 'db_username', 'db_password', 'db_name');
/* Now we filter all the elements which are NOT null - so we are left with an array
which holds only the missing arguments */
$db_info_array = array_filter($db_info_array, 'is_null');
/* If one or more arguments were missing, the next 'if' will execute */
if (count($db_info_array) !== 0)
{
/* Create an array from the keys of '$db_info_array' */
$missing_info = array_keys($db_info_array);
/* Converting it to a list of the missing settings separated by commas */
$missing_info = implode(', ', $missing_info);
/* Security... */
unset($db_host, $db_username, $db_password, $db_name, $db_info_array);
throw new exception('Some information is missing (' . $missing_info . ')');
}
/* If we reached here, all the required settings were found and therefore we try
to establish a connection */
//...
}So I thought about replacing the current error handling part (as in the above code) with this:
Code: Select all
public function __construct($db_host = null, $db_username = null, $db_password = null, $db_name = null)
{
if (func_num_args() !== 4)
{
// Don't know what to put here yet
}
//...
}