Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $clsDatabase = new clsDatabase();
- $user = new clsUser($clsDatabase);
- class clsDatabase{
- private $host;
- private $user;
- private $pass;
- private $dbname;
- private $dbh;
- private $error;
- /**
- * Database constructor.
- */
- public function __construct(){
- $this->host = 'localhost';
- $this->user = '1';
- $this->pass = '1';
- $this->db = '1';
- }
- /**
- * Database instance.
- */
- public function getDb(){
- // Set DSN
- $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
- // Set options
- $options = array(
- PDO::ATTR_PERSISTENT => true,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
- );
- // Create a new PDO instanace
- try {
- $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
- } // Catch any errors
- catch (PDOException $e) {
- $this->error = $e->getMessage();
- }
- }
- }
- class clsUser{
- private $dbh;
- public function __construct(clsDatabase $clsDB){
- $this->dbh = $clsDB->getDb();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement