turniej.unreal.pl :: Zobacz temat - [php5] Singleton
graphic logo logo graphic graphic  


[php5] Singleton
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:58 pm 

Raven
Admin
Admin


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


 
 
 

[php5] Singleton
Zobacz pojedynczy postZobacz pojedynczy post

     W tej chwili mamy klasę obsługującą bazę danych, którą możemy rozwijać o nowe funkcje. Jednakże tworzenie obiektu jest trochę uciążliwe - oto jak możemy je ułatwić.
     Singleton - pozwala na stworzenie instancji do obiektu, a tym samym używanie go w każdym miejscu na stronie.

plik: _handler.php
PHP:

<?php

&nbspDEFINE('DBPATH','./');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ścieżka do plików
&nbsp$php_ext='php';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp// rozszerzenie skryptów
&nbsp; require_once(DBPATH."class.db.interface".$php_ext);&nbsp; &nbsp;&nbsp; &nbsp;// interfejs bazy danych
&nbsp; require_once(DBPATH."class.db.basic.".$php_ext);&nbsp; &nbsp;&nbsp; &nbsp// podstawowa funkcja
&nbsp; if(file_exists(DBPATH."class.db.".$db_type.$php_ext))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// jeśli istnieje plik z obsługą bazy danych
&nbsp; &nbsp; &nbsp; require_once(DBPATH."class.db.".$db_type.$php_ext);&nbsp; &nbsp;// wczytuhemy go
&nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//w przeciwnym wypadku pokazujemy błąd
&nbsp; &nbsp; &nbsp; die("Could not obtain DB object.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp

class 
DBHandler
{&nbsp
&
nbsp; private static $db_object=null;&nbsp; &nbsp;// dzięki temu nie musimy tworzyć nowego obiektu za każdym razem, gdy chcemy go użyć
&nbsp
&
nbsp; public static function _create($config$type "")
&
nbsp; {
&
nbsp; &nbsp; &nbsp; &nbsp; if(!is_array($config)) return null;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//konfiguracja bazy danych musi być tablicą
&nbsp; &nbsp; &nbsp; &nbsp
&
nbsp; &nbsp;&nbsp; &nbsp;if(is_null(self::$db_object))&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// jeśli obiekt zawierający instancję jest pusty 
&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;&nbsp; &nbsp;
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* oraz istnieje klasa obsugująca bazę danych, możemy utworzyć instancję */
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(class_exists($typefalse)) self::$db_object = new $type;
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* jeśli operacja się powiodła, możemy klasę skonfigurować*/
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if(!is_null(self::$db_object)) self::$db_object -> db_configure($config);
&
nbsp; &nbsp; &nbsp; &nbsp; }
&
nbsp; &nbsp; &nbsp; &nbsp// jeśli instancja istnieje, po prostu ją zwracamy.
&nbsp; &nbsp; &nbsp; &nbsp; return self::$db_object;
&
nbsp; }
}
?>



Teraz wystarczy, że gdziekolwiek w kodzie wywołamy:

PHP:

<?php
&nbsp$db_config=array( 'server'=>'localhost'
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'user'=>'root'
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'pass'=>''
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'db'=>'baza11e'
&
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'persistent_conn' => 'false');
&
nbsp$db_type='mysql'//rodzaj bazy danych
&nbsp$db DBHandler::_create($db_config$db_type);&nbsp; &nbsp// tworzy obiekt
&nbsp; unset($db_config);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp// kasuje konfigurację
&nbsp; if(!is_object($db))&nbsp; &nbsp;&nbsp; &nbsp;// jeśli obsługa bazy danych nie została zainicjowana, zwracamy błąd
&nbsp; {
&
nbsp; &nbsp;die("BŁĄD: NIE MOŻNA UTWORZYĆ OBSŁUGI BAZY DANYCH.");
&
nbsp; }
&
nbsp$db->db_connect();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// łączymy się z bazą danych
&nbsp; if($db->message != "")&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// jeśli wystąpił błąd - zwracamy go
&nbsp; {
&
nbsp; &nbsp; &nbsp;die($db->message);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp
&
nbsp; }
?>


 
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