Jump to content

Recommended Posts

Hi,

 

How do you folks handle your database connection objects inside classes?  I use the ADODB php classes and an old abstraction db class called phpDB.

 

For example I have a class calle dsecurity, this contains a bunch of camera

objects in an array (totally making these classes up).  These classes store

and retrieve data from a database, the same one all the controlling code

uses.

 

What I have been doing is pass the db object in on the constructor so

inside the class I have $this->db to use and execute queries. 

 

Also have to pass it to each camera constructor inside the security class

 

for whatever {

$this->cameras[] = new camera($this->db, blah, blah);

}

 

This seems ok as far as resources as i'm passing by reference.  The only

thing I don't like is when I serialize or output the objects with print_r()

I get all the database object stuff in every instance of camera, annoying.

 

Wondering if there is a better way than having a reference to the database object in every

instance of a class.

 

Link to comment
https://forums.phpfreaks.com/topic/206494-database-objects-inside-classes/
Share on other sites

class CameraDAO {
  private $db;
  
  public function findById($id) {
    $row = $this->db->fetchRow('..');
    return new Camera($row['id'], $row['price'], ..);
  }
  
  public function findByPriceRange($low, $high) {
    $rows = $this->db->fetchAll('.. BETWEEN $low AND $high');
    foreach($rows as $row) {
      $cameras[] = new Camera($row['id'], $row['price'], ..);
    }
    return $cameras;
  }
  
  public function save(Camera $c) {
    $row['id'] = $c->getId();
    $row['price'] = $c->getPrice();
    ..
    $this->db->insert('table', $row);
  }
}

class Camera {
  private $id = 0;
  private $price = 0.0;
  private $angle = 0;
}

 

When you design a system you ideally want the classes to not know the names of the tables/columns because in the future these may be adjusted and for some reason (switch from MySQL to XML for example) these may not be the same which leads to the problem that you would have to find all occurrences of these column names in your classes. In the Java world they use a DAO (Data-Access Object) which fills the gap between your models and your database. As you can see only my DAO knows what the names of the columns are, my Camera class doesn't. All Camera knows is the attributes it shall have but not how it is named.

 

For further information:

http://en.wikipedia.org/wiki/Data_access_object

http://en.wikipedia.org/wiki/Object-relational_mapping

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.