Цитата
Sham:
не выдумывайте, после global всё выводится
PHP область видимости переменной
Ну да... пример взял из головы, не проверив.
Не работает в более сложном варианте.
читать дальше »
index.php.htm
Код: Выделить всё
<?php
require 'classes\app.php';
app::Run('project1','cfg');
app.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
$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
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'];
так вот, функция
Код: Выделить всё
spl_autoload_register(function ($class) {
global $path;
require '/' . $path['classes'] . '/' . $class . '.php';
});
не видит переменную $path
идем дальше
group.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 не помогает. Объясните мне в чем дело, я запутался.