<?php class DB { protected $db, $select_db; private function DBEx($Ex){ } public function Connect($host, $port, $user, $password){ } public function isConnected(){ if(!$this->db) return FALSE; else return TRUE; } public function query($sql){ if($this->isConnected()){ { $i = 0; $i++; } return $table; } else $this->DBEx('DB_NOT_CONNECTED'); } } class User { function User(){ $this->db = $GLOBALS['DB']; } private function UserEx($Ex){ } public function find($name, $email = false){ if($email) $res = $this->db->query('SELECT * FROM `user` WHERE `email` = ''.$name.'''); else $res = $this->db->query('SELECT * FROM `user` WHERE `name` = ''.$name.'''); foreach($res as $user); { $this->data['id'] = $user['id']; $this->data['name'] = $user['name']; $this->data['password'] = $user['password']; $this->data['salt'] = $user['salt']; $this->data['email'] = $user['email']; $this->data['confirm_mail'] = $user['confirm_mail']; $this->data['points'] = $user['points']; $this->data['access'] = $user['access']; } } } $GLOBALS['DB'] = new DB(); $DB->connect('host','port','user','haslo') $user = new User(); $user->find('nick'); ?>
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.