Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Exception - własna obsługa błędów., Błąd w klasie.
starach
post
Post #1





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

Ostrzeżenie: (0%)
-----


Witam mam oto taki kod php:
  1. <?php
  2. class Error
  3. {
  4. protected $message='Unknown exception';
  5. protected $desc;
  6. protected $code;
  7. protected $file;
  8. protected $line;
  9. protected $trace;
  10.  
  11. public function __construct(string $desc=NULL, int $code=NULL, string $message=NULL)
  12. {
  13. if(!empty($message) && $message !== 0)
  14. {
  15. $this->message = $message;
  16. }
  17. $this->code = $code;
  18. $this->file = __FILE__;
  19. $this->line = __LINE__;
  20. $this->trace = debug_backtrace();
  21. }
  22. final function get_message() {
  23. return $this->message;
  24. }
  25. final function get_desc() {
  26. return $this->desc;
  27. }
  28. final function get_code() {
  29. return $this->code;
  30. }
  31. final function get_file() {
  32. return $this->file;
  33. }
  34. final function get_line() {
  35. return $this->line;
  36. }
  37. final function get_trace() {
  38. return $this->trace;
  39. }
  40. public function __toString()
  41. {
  42. return '<div style="width:90%; margin:0 auto; padding:5px; background-color:#555; border:1px solid #000;">
  43. <b>Exception '.$this->get_code().':</b> '.$this->get_desc().
  44.  '<br><b>File:</b> '.$this->get_file().' <b>Line:</b> '.$this->get_line().
  45.  '<br><b>Error:</b> '.$this->get_desc();
  46. }
  47. }
  48. ?>
Jest to dosłownie odrobinę zmieniona klasa Exception.
Wywołanie błędu:
  1. <?php
  2. public function connect($db_host,$db_user,$db_pass,$db_name,$db_port)
  3. {
  4. try 
  5. {
  6. $this->db_host = $db_host;
  7. $this->db_port = $db_port;
  8. $this->db_name = $db_name;
  9.  if(true == ($this->db_link = @mysqli_connect($db_host,$db_user,$db_pass,$db_name,$db_port)))
  10. {
  11. return true;
  12. } else {
  13. throw new Error('Connection Problem', 0);
  14. }
  15. } catch (Error $e) 
  16. {
  17. die($e->__toString());
  18. }
  19. }
  20. ?>
Wywołuję go specjalnie zmieniając nazwę użytkownika dostępu do bazy na niepoprawną.
Pojawia mi się błąd:
Cytat
Catchable fatal error: Argument 1 passed to Error::__construct() must be an instance of string, string given, called in D:\WWW\orglee\class\class_mysql.php on line 70 and defined in D:\WWW\orglee\class\class_exception.php on line 13

Co jest nie tak ? Dlaczego wyświetla mi ten błąd i co on oznacza ? Na googlach znalazłem tylko ten błąd w CMS Joomla (błąd dotyczył template'a) a rozwiązania błędu nie było.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 16:53