Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- start work with PHP OOP
- <?php
- //Config
- $db_user = 'root';
- $db_pass = '';
- $db_host = 'localhost';
- $db_name = 'test';
- class Database
- {
- private $Database;
- private static $instance;
- public static function instance()
- {
- if ( !self::$instance )
- self::$instance = new Database();
- return self::$instance;
- }
- public function connect($host, $user, $password, $name)
- {
- $this->db_link = mysql_connect($host, $user, $password);
- mysql_set_charset('utf8');
- mysql_select_db($name, $this->db_link);
- }
- public function query($quory)
- {
- $sql = mysql_query($quory);
- $row = mysql_fetch_array($sql);
- return $row;
- }
- }
- class Book extends Database
- {
- public function getData2()
- {
- $sql = $this->query('SELECT * FROM users WHERE price = "7"');
- return $sql['name'];
- }
- }
- $db = Database::instance();
- $db->connect($db_host, $db_user, $db_pass, $db_name);
- $b = new Book();
- $res = $b->getData2();
- print_r($res);
- ?>
- $myDb = new dbLayer($settings);
- $myBook = new book($myDb);
- $a = $myBook->getAllData($someParameter);
- in the constructor of book you save the reference to the dblayer...
- class book {
- var $dbTier;
- __constructor($db) {
- $this->dbTier = $db;
- ...
- class Book
- {
- var $db;
- function __construct() {
- $this->db = Database::instance();
- }
- }
Add Comment
Please, Sign In to add comment