Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. $clsDatabase = new clsDatabase();
  2. $user = new clsUser($clsDatabase);
  3.  
  4. class clsDatabase{
  5. private $host;
  6. private $user;
  7. private $pass;
  8. private $dbname;
  9. private $dbh;
  10. private $error;
  11.  
  12. /**
  13. * Database constructor.
  14. */
  15. public function __construct(){
  16. $this->host = 'localhost';
  17. $this->user = '1';
  18. $this->pass = '1';
  19. $this->db = '1';
  20. }
  21.  
  22. /**
  23. * Database instance.
  24. */
  25. public function getDb(){
  26. // Set DSN
  27. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  28. // Set options
  29. $options = array(
  30. PDO::ATTR_PERSISTENT => true,
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
  33. );
  34. // Create a new PDO instanace
  35. try {
  36. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  37. } // Catch any errors
  38. catch (PDOException $e) {
  39. $this->error = $e->getMessage();
  40. }
  41. }
  42. }
  43.  
  44. class clsUser{
  45. private $dbh;
  46. public function __construct(clsDatabase $clsDB){
  47. $this->dbh = $clsDB->getDb();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement