"And storing username/password inside class is not a very good idea for production code."
Good idea is to store database connection settings in *.ini files but you have to restrict access to them. For example this way:
my_setting.ini:
[database]
driver = mysql
host = localhost
;port = 3306
schema = db_schema
username = user
password = secret
Database connection:
<?php
class MyPDO extends PDO
{
public function __construct($file = 'my_setting.ini')
{
if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');
$dns = $settings['database']['driver'] .
':host=' . $settings['database']['host'] .
((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') .
';dbname=' . $settings['database']['schema'];
parent::__construct($dns, $settings['database']['username'], $settings['database']['password']);
}
}
?>
Database connection parameters are accessible via human readable ini file for those who screams even if they see one PHP/HTML/any_other command.