turniej.unreal.pl :: Zobacz temat - [php5] Klasa obsługująca bazę danych w oparciu o Interfejsy
graphic logo logo graphic graphic  


[php5] Klasa obsługująca bazę danych w oparciu o Interfejsy
Napisz nowy temat   Odpowiedz do tematu
   Forum turniej.unreal.pl Strona Główna -> PHP, MySQL & Apache -> Tutoriale  
Autor Wiadomość
 Post Wysłany: Wto Gru 11, 2007 4:43 pm 

Raven
Admin
Admin


Dołączył: 13 Maj 2004
Posty: 200


 
 
 

[php5] Klasa obsługująca bazę danych w oparciu o Interfejsy
Zobacz pojedynczy postZobacz pojedynczy post

     Interfejs jest to zbiór funkcji, które muszą zostać zaimplementowane w klasie, która go wykorzystuje. Generalnie dużą zaletą interfejsów jest możliwość narzucenia jak jakie funkcje muszą zostać w klasie zaimplementowane i jak mają wyglądać. Wyobraźmy sobie klasę obsługującą bazę danych. Musi ona być tak napisana, aby bez względu na to, z jakiej bazy korzystamy (MySQL, PostgreSQL/MsSQL/Oracle) zawsze otrzymywać te same wyniki jednocześnie nie być zmuszonym do zmiany właściwego kodu aplikacji.

     Tworząc obsługę bazy danych należy zastanowić się jak ona ma wyglądać. Można zrobić abstrakcyjną klasę podstawową, w której będzie się inicjowało zmienne, oraz klasy potomne - zawierające właściwą obsługę danej bazy danych.

plik: class.db.basic.php
PHP:

<?php
abstract class basic
{
&
nbsp; protected $server;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp//server
&nbsp; protected $user;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp//uzytkownik bazy danych
&nbsp; protected $pass;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp//hasło bazy danych
&nbsp; protected $db;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp//nazwa bazy danych
&nbsp; protected $persistent_conn;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//połączenie stałe ?
&nbsp; public $message;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp//bład

&nbsp; protected $connection_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//numer połączenia
&nbsp; public $query_result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//wynik zapytania

&nbsp; final public function db_configure($config)
&
nbsp; {
&
nbsp; &nbsp; &nbsp; &nbsp$this->server=$config['server'];
&
nbsp; &nbsp; &nbsp; &nbsp$this->user=$config['user'];
&
nbsp; &nbsp; &nbsp; &nbsp$this->pass=$config['pass'];
&
nbsp; &nbsp; &nbsp; &nbsp$this->db=$config['db'];
&
nbsp; &nbsp; &nbsp; &nbsp$this->persistent_conn=$config['persistent_conn'];
&
nbsp; &nbsp; &nbsp; &nbsp; unset($this->query_result);
&
nbsp; &nbsp; &nbsp; &nbsp; unset($this->connection_id);
&
nbsp; }
}
?>



mamy podstawową klasę. Każda klasa potomna będzie dziedziczyć funkcję db_configure. Pomyślmy, jak mógłby wyglądać projekt interfejsy:

plik: class.db.interface.php
PHP:

<?php
interface db
{
&
nbsp; public function db_configure($config);
&
nbsp; public function db_connect();
&
nbsp; public function db_query($query "");
&
nbsp; public function db_close();
}
?>



W takim wypadku klasa obsługująca bazę będzie wyglądać tak:

plik: class.db.mysql.php
PHP:

<?php
class mysql extends basic implements db
{
&
nbsp; &nbsp;
&
nbsp; public function db_connect()
&
nbsp; {
&
nbsp; &nbsp$this->message="";
&
nbsp; &nbsp; if($this->persistent_conn)
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@$this->connection_id=mysql_pconnect($this->server$this->user$this->pass) or $this->message.="Error ".mysql_errno().": Can not connect to MySQL <br>";
&
nbsp; &nbsp; else
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@$this->connection_id=mysql_connect($this->server$this->user$this->pass) or $this->message.="Error ".mysql_errno().": Can not connect to MySQL <br>";
&
nbsp; &nbsp; if($this->message == "") @Mysql_select_db($this->db) or $this->message.="Error ".mysql_errno().": Can not connect to database <br>";
&
nbsp; }
&
nbsp; public function db_query($query "")
&
nbsp; {
&
nbsp; &nbsp; &nbsp;unset($this->query_result);
&
nbsp; &nbsp;if($query != "")
&
nbsp; &nbsp;{
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->query_result = @mysql_query($query$this->connection_id);
&
nbsp; &nbsp;}
&
nbsp; &nbsp;if($this->query_result)
&
nbsp; &nbsp;{
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this->query_result;
&
nbsp; &nbsp;}
&
nbsp; &nbsp;else
&
nbsp; &nbsp; {
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;
&
nbsp; &nbsp; }
&
nbsp; }
&
nbsp; public function db_close()
&
nbsp; {
&
nbsp; &nbsp;if($this->connection_id)
&
nbsp; &nbsp;{
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(IsSet($this->query_result))
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;@mysql_free_result($this->query_result);
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;unset($this->query_result);
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$result=@mysql_close($this->connection_id);
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unset($this->connection_id);
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $result;
&
nbsp; &nbsp; }
&
nbsp; &nbsp; else
&
nbsp; &nbsp; {
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;
&
nbsp; &nbsp; }
&
nbsp; }
}
?>



Powyższa klasa nie ma zaimplementowanej funkcji db_configure gdyż dziedziczy ją od swojej klasy macierzystej.

 
Odpowiedz z cytatem

_________________

personal wiki
Zobacz profil autora Wyślij prywatną wiadomość Wyślij email Odwiedź stronę autora Numer GG Tlen
   Forum turniej.unreal.pl Strona Główna -> PHP, MySQL & Apache -> Tutoriale
Napisz nowy temat   Odpowiedz do tematu
Skocz do:  

 Further options
 Wszystkie czasy w strefie GMT
Strona 1 z 1
 
Zobacz poprzedni temat :: Zobacz następny temat  
Nie możesz pisać nowych tematów
Nie możesz odpowiadać w tematach
Nie możesz zmieniać swoich postów
Nie możesz usuwać swoich postów
Nie możesz głosować w ankietach
Nie możesz dołączać plików na tym forum
Nie możesz ściągać plików na tym forum
Wyświetl posty z ostatnich:   
 

turniej.unreal.pl Kanał RSS tematu 
PicLens 

Powered by phpBB © 2001, 2002 phpBB Group
Illusion template v.1.0.2 © Jasidog.com
Powered by phpbb, copyright the phpbb group Template by jasidog.com