Korzystam z klasy EzSql do laczenia sie z baza MySQL.
Utworzylem swoja wlasna klase, ktora jest ekstensja klasy EzSQL - glownie po to aby dorzucic obsluge wyjatkow.
I mam kilka pytan.
Kod klasy wyglada tak:
include 'ez_sql_mysql.php'; interface IException { /* Protected methods inherited from Exception class */ public function getMessage(); // Exception message public function getCode(); // User-defined Exception code public function getFile(); // Source filename public function getLine(); // Source line public function getTrace(); // An array of the backtrace() public function getTraceAsString(); // Formated string of trace /* Overrideable methods inherited from Exception class */ public function __toString(); // formated string for display public function __construct($message = null, $code = 0); } abstract class CustomException extends Exception implements IException { protected $message = 'Unknown exception'; // Exception message private $string; // Unknown protected $code = 0; // User-defined exception code protected $file; // Source filename of exception protected $line; // Source line of exception private $trace; // Unknown public function __construct($message = null, $code = 0) { if (!$message) { throw new $this('Unknown '. get_class($this)); } parent::__construct($message, $code); } public function __toString() { return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n" . "{$this->getTraceAsString()}"; } } class TestException extends CustomException {} class DB Extends ezSQL_mysql{ public $wiersz= ''; public $wartosc=''; public function __construct(){ try{ include CONFIG_DIR.'db.inc'; $dbuser=$CONFIG['sql_user']; $dbpassword=$CONFIG['sql_pass']; $dbname=$CONFIG['sql_dbname']; $dbhost=$CONFIG['sql_host']; parent::ezSQL_mysql(); parent::connect($dbuser, $dbpassword, $dbhost); parent::select($dbname); if(parent::select($dbname) == false) throw new TestException('Blad polaczenia z baza !'); } catch (TestException $e) { } } public function query_row($kod){ try{ return parent::get_row($kod); if(parent::query($kod) == false) throw new TestException('Bledne zapytanie !'); } catch (TestException $e) { } } public function query_value($kod){ try{ $this->wartosc = parent::get_var($kod); if(parent::query($kod) == false) throw new TestException('Bledne zapytanie !'); } catch (TestException $e) { } } }
Konstruktor klasy DB tworzy i sprawdza polaczenie do bazy i tu nie mam pytan.
Natomiast metody query_row i query_value wykorzystuja oryginalne metody klasy EzSQL ale sa rozszerzone o obsluge try catch. No i pytanie jak zwrocic wartosci wyciagane przez moje metody. O ile w przypadku query_value nie ma specjalnie problemu bo zwraca ona stringa, to w przypadku query_row zwraca obiekt.
Czy poprawnie jest zwrocenie obiektu przez return ? Jak to powinno poprawnie wygladac?