Mahesh
Mahesh

Reputation: 1593

PHP and Mysqli OOP - Handling database

Today i tried to convert my functions to PHP Class. I tried with some basic steps.

<?php
    class DataBase {
        private $host;
        private $user;
        private $password;
        private $db;
        private $mysqli;

        function __construct() {
            $this->host = "localhost";
            $this->user = "root";
            $this->password = "";
            $this->db = "my_database";

            $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
        }

        function __destruct() {
            $this->mysqli->close();
        }

        public function query($query, $params = '', $bind_result) {
            $stmt = $this->mysqli->prepare($query);

                    //Need to change this to process the array of params
            $stmt->bind_param('i', $params);
            $stmt->execute();
                    //Change this to handle array of bind
            $stmt->bind_result($bind_result);
                    //Loop the result and store it in a temp array
            $stmt->fetch();
                    //Don't print the statement. Just close the statement and return the array.
            printf("%s\n", $bind_result);
            /* close statement */
            $stmt->close();
        }
    }
?>

I have to now create another class. I created one dummy table in database.

<?php
    class Dummy {

        private $database;

        function __construct() {
            $this->database = new Database();
        }

        public function getAllDummy() {
            $query = "SELECT name FROM dummy WHERE id = ?";
            $this->database->query($query, 1, 'name');
        }

    }
?>

But I don't think it is the right way to do the things. I feel some of them are in the right way and some of them are wrong.

When i call the query() function, Do i need to connect the database all the time in every classes' construct method? Or Do i need to change the Database class to static functions? So i can call the functions like Database::query();

It seems i need to create everything from the start. Is such a model already available in internet? like cakephp, codeigniter

Upvotes: 3

Views: 3311

Answers (2)

BIOHAZARD
BIOHAZARD

Reputation: 2043

Besides Doctrine (Which is powerfull and almighty already but still to complex) I can suggest you db.php (http://dbphp.net) which does everything what doctrine but is single file and is very easy to use. Cons: It is not well documented yet and has no big community yet.

Upvotes: 0

Tomas Zaruba
Tomas Zaruba

Reputation: 196

I would like to recommend you to read something about ORM for PHP. For example I m using Doctrine 2 (http://www.doctrine-project.org/) It is kinda complex but definitely worth to learn. There is everything you are trying to code already done, so why you should make it again?

In your OOP principe there are some mistakes.

  • You are creating Database instance for every class like Dummy, if you will have class Users, Articles, you will create 3x Database, it isnt really good. You should make Database as service or Singleton and make it just once. Good solution for this can be Dependency injection (http://en.wikipedia.org/wiki/Dependency_injection).
  • Also I would recommend you to generalize whole Dummy class, to make it more general. Dont make method "getAllDummy" but for example "getAll($tableName)"so you can use it for every table.

Upvotes: 2

Related Questions