![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Przyjaciele php.pl Postów: 215 Pomógł: 0 Dołączył: 28.10.2003 Skąd: - Ostrzeżenie: (0%) ![]() ![]() |
[php:1:f3d8be1558]<?php
class usersAuthenticate { var $login; var $password; var $db; function loginUser() { if(empty($this->login) || empty($this->password)) { return(false); } else { $row = $this->db->GetRow("SELECT password, date, userName FROM users WHERE login='".$this->login."'"); } if($row[0] != md5($this->password)) { return(false); } else { $this->db->Execute("UPDATE users SET session='".session_id()."', ip='".$_SERVER['REMOTE_ADDR']."', date=NOW() WHERE login='".$this->login."'"); } if(!$this->db->Affected_Rows()) { return(false); } else { $_SESSION['date'] = $row[1]; $_SESSION['pelnaNazwa'] = $row[2]; return(true); } } ?>[/php:1:f3d8be1558] Czy odwolanie sie do ADODB (po przez $db) w tym fragmencie klasy to jedyny sposob? Czy jest to optymalne rozwiazanie? Czy mozna zrobic to w inny sposob np. przez "extends"? Jakie są Wasze rozwiązania i doświadczenia? |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 90 Pomógł: 0 Dołączył: 3.04.2003 Skąd: Opole Ostrzeżenie: (0%) ![]() ![]() |
Cytat Moze tak: $this->db = DB::Instance;
to jest to samo co uzywanie zmiennej globalnej :) U siebie rozwiazuje to w ten sposob, ze tworze obiekt REJESTR, ktorego zadaniem jest przetrzymywanie danych globalnych. (Zawsze lepiej miec 1 zmienna globalna niz 10) [php:1:e66b336cd5] <?php /** * Provides a central store for global data and objects. * Method load() is for loading objects/data into the registry. * @access public * @package System */ class Registry { var $_dir; var $_loaded = array(); /** * @param string $dir (with trailing slash at the end) method load() will look for files to include in this directory * @access public */ function Registry($dir = null) { if (isset($dir)) { if (!file_exists($dir) || !is_dir($dir)) { return trigger_error("Registry::__contruct() failed, directory does not exist `$dir`", E_USER_ERROR); } $this->_dir = $dir; } } /** * Load file/files * @return void * @throws trigger_error() * @access public */ function load() { foreach (func_get_args() as $v) { if (in_array($v, $this->_loaded)) { continue; } $file = $this->_dir . $v . '.php'; if (!file_exists($file) || !is_file($file)) { return trigger_error("Registry::load('$v') failed, file does not exist", E_USER_ERROR); } require $file; $this->_loaded[] = $v; } } } ?> [/php:1:e66b336cd5] W pliku "includes/prepend.php" inicjalizowany jest Rejestr i obsluga bledow - includuje go w kazdym pliku. W katalogu "includes/registry-load/" sa pliki ktore odpowiadaja za wgrywanie tych obiektow. np: Db, Config, Unique, User Przykladowy plik "includes/registry-load/Db.php" [php:1:e66b336cd5] <?php import('system.Db'); global $Registry; $Registry->load('Config'); $Registry->Config->load(ROOT . 'config/db.ini'); $Registry->Db =& Db::factory($Registry->Config->get('db.driver')); ?> [/php:1:e66b336cd5] W skrypcie w szybki sposob mozna zaladowac obiekty/dane ktorych w danym momencie potrzebujesz: [php:1:e66b336cd5] <?php require 'includes/prepend.php'; $Registry->load('Db', 'Smarty', 'User'); ?> [/php:1:e66b336cd5] czy w klasie [php:1:e66b336cd5] cass Costam { function Costam() { global $Registry; $this->Db = $Registry->Db; $this->Smarty = $Registry->Smarty; } } [/php:1:e66b336cd5] |
|
|
![]() ![]() |
![]() |
Aktualny czas: 6.10.2025 - 05:55 |