At the end you have to put your connections configuration parameters inside your code. Doesn't matter if it is in a constant, a class, etc....
I don't think hardcoding those values a security risk. Your php scripts ar in the server. So they are protected by the file system permissions and the system permissions as well.
If somebody can access your files the security is issue is in the server and not in the php script.
Use PDO, it has classes to handle db access.
From the PDO php manual:
The PHP Data Objects (PDO) extension defines a lightweight, consistent
interface for accessing databases in PHP.
Connections are established by creating instances of the PDO base
class.
Example #1 Connecting to MySQL
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>