morebeauty |
31-05-2014 10:35 2358089 |
Цитата:
Цитата Sham
не выдумывайте, после global всё выводится »
|
Ну да... пример взял из головы, не проверив.
Не работает в более сложном варианте.
читать дальше »
index.php
PHP код:
<?php require 'classes\app.php'; app::Run('project1','cfg');
app.php
PHP код:
<?php class app{ private static $_instance; public static function getInstance(){ if(!self::$_instance){ self::$_instance = new self(); } return self::$_instance; } public static function Run($proj,$conf){ require "./protected/$proj/$conf.php"; require 'db.php'; $db = new PDO("mysql:host={$cfg['db_props']['host']};dbname={$cfg['db_props']['name']}",$cfg['db_props']['user'],$cfg['db_props']['pass']); $path = $cfg['path']; //$db = DB::getInstance(); //$db->setProps($cfg['db_props']); //$db=$db->connect(); require "./protected/$proj/site.php"; } }
cfg.php
PHP код:
<?php $cfg = array ( 'db_props' => array ( 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'name' => 'db1', 'prefix' => 'mm_' ), 'db_model' => array ( 'user' => array ( 'table' => 'mm_users', 'fields' => array ( 'id', 'login', 'password', 'email', 'owner', 'regdata', 'birthday', 'confirmed' ) ), 'group' => array ( 'table' => 'mm_groups', 'fields' => array ( 'id', 'group' ) ), ), 'path' => array ( 'classes' => 'classes', 'tpl' => 'templates' ), );
site.php
PHP код:
<?php SESSION_START();
$path = $cfg['path']; require '/' . $path['classes'] . '/dbobj.php';
//foreach ($cfg['db_model'] as $key => $value){ // require '/' . $path['classes'] . '/' . $key . '.php'; //} spl_autoload_register(function ($class) { global $path; require '/' . $path['classes'] . '/' . $class . '.php'; }); //echo $cfg['path']['classes']; $group = new group(1,$db); $curGroup=$group->getRow(); echo $curGroup['id'];
так вот, функция
PHP код:
spl_autoload_register(function ($class) { global $path; require '/' . $path['classes'] . '/' . $class . '.php'; });
не видит переменную $path
идем дальше
group.php
PHP код:
<?php class group extends dbobj{ private $row; private $stmt; public function __construct($id,$db){ parent::__construct(self); $this->db=$db; $this->stmt = array ( 'byid' => $db->prepare('SELECT :fld FROM :tbl WHERE id=:id'), ); //$row = self::$row; //$stmt = $db->prepare('SELECT :fld FROM :tbl WHERE id=:id'); $this->stmt['byid']->bindValue(':fld','*'); $this->stmt['byid']->bindValue(':tbl',$this->table); $this->stmt['byid']->bindValue(':id',$id); $this->stmt['byid']->execute(); echo $this->table; $this->row = $this->stmt['byid']->fetch(PDO::FETCH_ASSOC); } public function getRow(){ //echo "123"; return $this->row; } }
мне пришлось в конструктор передавать переменную $db, потому что global $db не работало. А переменная определена еще в файле app.php на стадии запуска (функция run). Внутри этой функции после определения переменной подключается файл site.php, в котором переменные $db и $cfg видны. Но вот в классах, которые подключаются в site.php уже даже global не помогает. Объясните мне в чем дело, я запутался.
|