I'm relatively new to OOP php and have been creating a bunch of classes (user class, validation class, etc). I have a separate php file (mysqli_connect.php) that resides in web root that all my scripts include. This mysqli_connect.php uses the mysqli OO connection, as opposed to the procedural.
Code: Select all
// {mysqli_connect.php}
// Instantiate MySQL connection object:
$dbc = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno())
{
throw new RuntimeException('Cannot access database: ' . mysqli_connect_error());
}I have a PHP book that uses a separate mysql_connect.php file, and does it this way:
Code: Select all
$GLOBALS['DB'] = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);What do you guys do? What do you suggest? I'm looking for a "proper" and professional way of sharing that mysqli connection object with all my classes. Thanks so much for your help and guidance.