Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Korzystanie z utworzonego obiektu w innej klasie.
Mr_KoKa
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 1.01.2009

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


Witam, nie dawno zająłem się klasami w PHP i po napisaniu klasy DataBase i klasy User pojawił się problem jak klasa User ma korzystać z utworzonego obiektu klasy DataBase, aby wykonywać operacje na bazie danych. Aby ominąć chwilowo problem napisałem takie coś:

  1. <?php
  2.  class DB {
  3.  
  4.      protected $db, $select_db;
  5.   
  6.      private function DBEx($Ex){
  7.          die($Ex);         
  8.      }
  9.   
  10.      public function Connect($host, $port, $user, $password){
  11.   
  12.          $this->db = mysql_connect($host.':'.$port, $user, $password);
  13.               
  14.      }
  15.      public function isConnected(){
  16.   
  17.          if(!$this->db) return FALSE;
  18.          else return TRUE;
  19.   
  20.      }
  21.   
  22.      public function query($sql){
  23.          if($this->isConnected()){
  24.              $result = mysql_query($sql, $this->db);
  25.              if(is_numeric($result)) return $result;
  26.              $table = array();
  27.              if (@mysql_num_rows($result) > 0)
  28.              {
  29.                  $i = 0;
  30.                  while($table[$i] = mysql_fetch_assoc($result)) 
  31.                      $i++;
  32.                  unset($table[$i]);                                                                                  
  33.              }                                                                                                                                     
  34.              @mysql_free_result($result);
  35.              return $table; 
  36.          } else $this->DBEx('DB_NOT_CONNECTED');
  37.      }
  38.   
  39.  }
  40.  
  41. class User {
  42.   
  43.      function User(){
  44.          $this->db = $GLOBALS['DB'];
  45.      }
  46.   
  47.      private function UserEx($Ex){
  48.          die($Ex);         
  49.      }
  50.   
  51.      public function find($name, $email = false){
  52.          if($email) $res = $this->db->query('SELECT * FROM `user` WHERE `email` = ''.$name.''');
  53.          else $res = $this->db->query('SELECT * FROM `user` WHERE `name` = ''.$name.''');
  54.          foreach($res as $user);
  55.          {
  56.              $this->data['id'] = $user['id'];
  57.              $this->data['name']    = $user['name'];
  58.              $this->data['password'] = $user['password'];
  59.              $this->data['salt'] = $user['salt'];
  60.              $this->data['email'] = $user['email'];
  61.              $this->data['confirm_mail'] = $user['confirm_mail'];
  62.              $this->data['points'] = $user['points'];
  63.              $this->data['access'] = $user['access'];
  64.          }
  65.       
  66.      }
  67. }
  68.  
  69.  
  70.  
  71. $GLOBALS['DB'] = new DB();
  72.  
  73. $DB->connect('host','port','user','haslo')
  74.  
  75. $user = new User();
  76.  
  77. $user->find('nick');
  78. ?>


I takie rozwiązanie się sprawdza, ale przeczytałem gdzieś, że nie jest poprawne, bezpieczne, etc.



Tu pojawia się moje pytanie, jak nie tak to zrobić, aby nie musieć przy każdym stworzeniu obiektu User() podawać w parametrze obiektu bazy danych?

Jestem początkujący w klasach, nie wiem czy dobrze wszystko nazwałem, ale myślę, że jakoś wytłumaczyłem, o co mi chodzi ;].

Dziękuję za przeczytanie tego postu.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 26.12.2025 - 04:43