www.webdeveloper.com
Results 1 to 2 of 2

Thread: OOP Database

  1. #1
    Join Date
    Jul 2014
    Posts
    6

    OOP Database

    Hello!

    I'm working on my next website.
    Never had to work on a maintainable code because never had to change so many things on my websites.
    Currently i have 3 abstract classes. (They have misleading names)

    AbstractDatabase - This class have a connection to the database (mysql) and returns the requested data.
    AbstractObject - This class represents a table in the database, like User
    AbstractObjectList - This class represents a table just like the AbstractObject but this class is supposed to work with multiple object.

    PHP Code:
        abstract class AbstractDatabase {
            
            protected 
    $table;
            protected 
    $db;
            
            public function 
    AbstractDatabase($table$db) {
                
    $this->table $table;
                
    $this->db $db;
            }
            
            public function 
    selectAll($orderby=NULL$offset=NULL$limit=NULL) {
                
    $query "SELECT * FROM {$this->table}";
            
                if (
    $orderby$query .= " ORDER BY $orderby";
                if (
    $offset !== NULL && $limit !== NULL$query .= " LIMIT $offset$limit";
                
                
    $q $this->db->prepare($query);
                
    $q->execute();
                
                return 
    $q->fetchAll(PDO::FETCH_OBJ);
            }
            
            public function 
    countPages($perpage) {
                
    $q $this->db->prepare("SELECT COUNT(*) FROM {$this->table}");
                
    $q->execute(array($perpage));
                
                return 
    ceil($q->fetchColumn() / $perpage);
            }
        } 
    PHP Code:
        abstract class AbstractObjectList {
            
            public abstract function 
    existsInDatabase();
            public abstract function 
    readFromDatabase();
            public abstract function 
    writeToDatabase();
            public abstract function 
    deleteFromDatabase();
            
        } 
    PHP Code:
           abstract class AbstractObjectList {
            
            protected 
    $rows = array();
            
            public function 
    get($index) {
                return 
    $this->rows[$index];
            }
            
            public function 
    getAll() {
                return 
    $this->rows;
            }
            
        } 
    So what is my main problem?
    I think this is a very bad design, why?
    Because i have a static variable at every 'class extends AbstractObject' which is called $defaultDb, this $defaultDb is an instance of the corresponding 'AbstractDatabase'.
    Examlple:

    PHP Code:
    class UserDatabase extends AbstractDatabase {
        
        public function 
    UserDatabase($db) {
            
    parent::__construct("users"$db);
        }
        
        public function 
    selectById($id) {
            
    $q $this->db->prepare("SELECT * FROM {$this->table} WHERE id=? LIMIT 1");
            
    $q->execute(array($id));
            
            return 
    $q->fetchObject();
        }

    .....

    PHP Code:
    class User extends AbstractObject {
        
        public static 
    $defaultDb;
        
        public static function 
    setDefaultDatabase($db) {
            
    self::$defaultDb $db;
        }
        
        protected 
    $id "";
        protected 
    $email "";
        protected 
    $password "";
        protected 
    $fullname "";
        
        public function 
    User($id ""$email ""$password ""$fullname "") {
            
    $this->id $id;
            
    $this->email $email;
            
    $this->password $password;
            
    $this->fullname $fullname;
        }
        
        public function 
    existsInDatabase($db=NULL$set=true) {
            if (
    $db == NULL$db self::$defaultDb;
            
            if (empty(
    $this->id) && empty($this->email)) return false;
        
            try {
                
    $this->readFromDatabase($db$set);
            } catch(
    Exception $ex) {
                return 
    false;
            }
            
            return 
    true;
        } 
    Why did i use static?
    Because whenever i want to access a user from the database i have to:
    PHP Code:
    $userDb = new UserDatabase($mysqldatabase);
    $user = new User(1); // Create a new user with id 1
    $user->readFromDatabase($userDb); // Read $user from $userDb, $user- 
    How can i improve this code? I used to code in Java but somehow i can't do OOP websites in PHP, it just doesn't become more hard to maintain the code in the later, NOT EASIER.

    Please help, thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,355
    I would probably just pass in your DB object as a parameter for the constructor of any class that needs it. Whether it belongs in the abstract class's constructor depends on whether they all use the DB or not. (If not, you may need a child abstract class for those that use the DB?) Then you only need to instantiate your DB class once, and pass it into any other class that needs it (by reference by default).

    Also, if you have an abstract class that only defines abstract methods, that's an excellent candidate to be an interface, instead (allowing you to actually extend one class while also implementing that interface. (I'm not sure which class name is affected, as you used AbstractObjectList for 2 different class definitions here.)

    PHP Code:
    $db = new WhateverYourDBClassIs();
    $foo = new Foo($db);
    $bar = new Bar($db);
    // etc.... 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles