简体   繁体   中英

PHP OOP: How do I create a database class?

Currently, my website runs procedural PHP. I would like to achieve to have a database class that can be used for other classes.

STATUS QUO :

On every page I include my dp.php before my header.php, the content and footer.php appear. My db.php looks like this:

// Credentials
$dbhost = "localhost";
$dbname = "dbname";
$dbuser = "dbuser";
$dbpass = "dbpassword";

//  Connection
global $db;

$db = new mysqli();
$db->connect($dbhost, $dbuser, $dbpass, $dbname);
$db->set_charset("utf8");

//  Check Connection
if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
exit();
}

GOAL:

Having a database object that I can integrate into the EventData class that I have been building. The desired function of this class is that it will allow me to easily access data of my events table on any page. My EventData-class.php looks like this:

Class EventData {

private $_db;
private $_event_id;

public function __construct($eventID) {
    $this -> _event_id = $eventID;
}

public function getValue($fieldname) {

    // Build query for getting event details
    $query = 'SELECT * FROM events WHERE id=' . $this -> _event_id . '';

    // Do Search
    $results = $db->query($query);

    // Store all event details available in variables
    while ($result = $results->fetch_assoc()) {
        $value = $result[$fieldname];
    }

    // Return value
    return $value;

}

}

I am new to OOP and am interested in learning how I need to modify both db.php and EventData-class.php in order to work.

The answer is brief and needs some your effort to improve or encode the idea.

First of all create a collection of items, something like this

class EventCollection {
  protected $items = array(); // I keep protected so only addItem, removeItem etc. can be used to handle data
  public $database = NULL;

  public function addItem(EventData $item){
    $this->items[] = $item;
    $item->collection = $this; // this allows your EventData object to refer to its container
  }
  public function clear(){
    $this->items = array();
  }

  public function __construct(mysqli $datb){ // inject mysqli object
    $this->database = $datb;
  }

  public function removeItem... etc.
}

Having prepared the collection class, just update your EventData (consider creating a child class) by $collection field:

class EventData {

  private $_db; // throw this away, not necessary
  private $_event_id;
  public $collection = NULL;
  // etc.

Then in your main code create a $db variable as you do. Then create a (empty) collection:

 $myEventCollection = new EventCollection($db);

and add items by

 $myEvent = ....;
 $myEventCollection->add($myEvent);

To refer to $db value inside the EventData class use something like this

 class EventClass { ....
 public function updateEvent(){
   $this->collection->database->query('UPDATE ...');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM