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]