na wstępie przepraszam jeśli nie ten działa, ale to co zrobiłem traktuję jako naukę (stąd znalazło się to w przedszkolu a nie w ocenach) dlatego proszę o porady co robię źle, czy to co w ogóle zrobiłem ma sens.
Chcę nauczyć się programowania obiektowego w php... najlepiej według mnie uczy się rozwiązując jakiś problem / pisząc jakiś skrypt... dlatego też postanowiłem, że pod swojego CMS (jak i sam system przerobie pod obiektów-kę) napiszę główny silnik do obsługi zapytań do bazy danych (kolejne klasy będą rozszerzeniem tego silnika - np. wczytywanie/dodawanie/edycja newsów). Szperałem w internecie na temat samego OOP i natknąłem się na bibliotekę PDO. Na podstawie tego co wyczytałem w php.net stworzyłem obiektowo (tak mi się wydaje) główny silnik do obsługi mojego systemu CMS. Teraz bardzo proszę "wyjadaczy" o porady czy to co zrobiłem jest OK, co należy zmienić czego unikać.
KOD:
<?php /* * Silnik Systemu v. 0.1a */ class DBEngine{ private $engine; private $host; private $database; private $user; private $pass; private $codetype; public $db; public $errors; public function __construct($dbCONFIG = NULL){ if($dbCONFIG == NULL){ $this->engine = 'mysql'; $this->host = 'localhost'; $this->database = 'test'; $this->user = 'root'; $this->pass = ''; $this->codetype = 'utf-8'; } else{ $this->engine = $dbCONFIG[0]; $this->host = $dbCONFIG[1]; $this->database = $dbCONFIG[2]; $this->user = $dbCONFIG[3]; $this->pass = $dbCONFIG[4]; $this->codetype = $dbCONFIG[5]; } $dns = $this->engine . ':dbname=' . $this->database . ';host=' . $this->host; $dbuser = $this->user; $dbpass = $this->pass; try { $this->db = new PDO($dns,$dbuser,$dbpass); self :: setEncoding($this->codetype); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } private function setEncoding($codetype = 'utf-8'){ try { $sth = $this->db->prepare('SET NAMES :codetype; SET CHARACTER SET :codetype'); $sth->bindParam(':codetype', $codetype, PDO::PARAM_STR); $sth->execute(); $sth->closeCursor(); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } public function query($sql = NULL){ if($sql != NULL){ try { $sth = $this->db->prepare($sql); $sth->execute(); $sth->closeCursor(); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined queries'; } } public function fetch($sql = NULL, $type = 1){ if($sql != NULL){ try { $sth->self :: query($sql); switch($type){ case '1': $result = $sth->fetch(PDO::FETCH_ASSOC); break; case '2': $result = $sth->fetchAll(); break; } $sth->closeCursor(); return $result; } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined queries'; } } public function fetchArray($argument = NULL, $table = NULL, $order = 'id', $type = 'DESC', $limit = NULL, $where = NULL){ if($argument != NULL && $table != NULL){ try { if($limit != NULL || $where != NULL){ if($limit != NULL && $where != NULL){ $sth = $this->db->prepare("SELECT $argument FROM $table WHERE $where ORDER BY $order $type LIMIT $limit"); } elseif($limit != NULL && $where == NULL){ $sth = $this->db->prepare("SELECT $argument FROM $table ORDER BY $order $type LIMIT $limit"); } else{ $sth = $this->db->prepare("SELECT $argument FROM $table WHERE $where ORDER BY $order $type"); } } else{ $sth = $this->db->prepare("SELECT $argument FROM $table ORDER BY $order $type"); } $sth->execute(); $result = $sth->fetchAll(); $sth->closeCursor(); return $result; } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined arguments or table'; } } public function fetchRow($argument = NULL, $table = NULL, $id = NULL, $where = ''){ if($argument != NULL && $table != NULL && $where != NULL){ try { $sth = $this->db->prepare("SELECT $argument FROM $table WHERE id=:id $where"); $sth->bindValue(":id", $id, PDO::PARAM_INT); $sth->execute(); $result = $sth->fetch(PDO::FETCH_ASSOC); $sth->closeCursor(); return $result; } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined arguments or table or where'; } } public function insert($table = NULL, $data = NULL){ if($table != NULL && $data != NULL){ $columns = ''; $values = ''; try { foreach ($data as $column => $value) { $columns .= ($columns == '') ? '' : ', '; $columns .= $column; $values .= ($values == '') ? '' : ', '; $values .= ":$value"; } $sth = $this->db->prepare("INSERT INTO $table ($columns) VALUES ($values)"); foreach ($data as $column => $value) { $sth->bindValue(":$value", $value, PDO::PARAM_STR); } $sth->execute(); $sth->closeCursor(); return $this->db->lastInsertId(); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined table or data'; } } public function update($table = NULL, $values = NULL, $id = NULL){ if($table != NULL && $id != NULL){ try { $sth = $this->db->prepare("UPDATE $table SET $values WHERE id=:id"); $sth->bindValue(":id", $id, PDO::PARAM_INT); $sth->execute(); $sth->closeCursor(); return true; } catch (PDOException $e) { return true; $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined table or values or id'; } } public function delete($table = NULL, $id = NULL){ if($table != NULL && $id != NULL){ try { $sth = $this->db->prepare("DELETE FROM $table WHERE id=:id"); $sth->bindValue(":id", $id, PDO::PARAM_INT); $sth->execute(); $sth->closeCursor(); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined table or id'; } } if($sql != NULL){ try { $sth = $this->db->prepare($sql); switch($type){ case 'col': $count = $sth->columnCount(); break; case 'row': $sth->execute(); $count = $sth->rowCount(); break; } return $count; $sth->closeCursor(); } catch (PDOException $e) { $this->errors[] = $e->getMessage(); } } else { $this->errors[] = 'Not defined queries'; } } public function getError(){ $view = ''; $view = '<ul>'; foreach($this->errors as $error){ $view .= '<li>' . $error . '</li>'; } $view .= '</ul>'; } return $view; } public function __destruct(){ $this->db = NULL; } } ?>
Dziękuje za wszelkie uwagi i proszę o wyrozumiałość.
Pozdrawiam Grzegorz
