Forum Webscript.Ru

Программирование => PHP => Тема начата: Voldemar от 15 Декабря 2004, 17:20:04

Название: ссылочки в PHP
Отправлено: Voldemar от 15 Декабря 2004, 17:20:04
Всем привет.
пишу на PHP давно, но тут голову сломал пытаясь решить простую проблему:
строю дерево. для удобства навигации надо хранить еще и parent для каждой ноды.
XML использовать возможности нет.
вот кусок кода (максимально его облегчил):
class dir {
function &dir($id=0, $level=0,$name=\'Root\',&$parent) {
$this->id = $id;
$this->childs = array();
$this->name = $name;
$this->childCount = 0;
$this->parent = &$parent;
$this->level=$level;
$this->readChilds($id);
}


function &readChilds($id) {
$sql = "select * from dirs where parent ".($this->level==0?"IS NULL":"=$id")." order by shortname limit 1";
$q = new query($sql);
while (($row = $q->fetch())) {
$id = $row [\'cid\'];
$level = $row[\'level\'];
$obj = &$this;
$this->childs[]  = new dir($id,$level,$row[\'name\'],&$obj);
}
}

}
class dirs  {
function dirs() {
$this->current = false;
additional::db();
$temp = false;
$this->root = new dir(0, 0,\'Root\',$temp); // read root dir
}
}

$d = new dirs();
$d->root->name=\'aaaaaa\';
print_r($d);
exit;


хотелось, конечно же хранить ссылки на объекты в parent, но сейчас это копируется. почему - понять не могу. для проверки меняю $d->root->name после того как дерево сформировано.

написал более простой пример, оно работает как надо:

class test {
function &test ($name,&$parent) {
$this->name = $name;
$this->parent = &$parent;
}

}

class test2 {
    function test2() {
$this->testname = \'aaaa\';
$this->test = new test(\'aaa\',$this);
$this->testname = \'bbbb\';
}
}

$q = new test2();
print_r($q);
?>


какие мнения будут у уважаемой общественности?
Название: ссылочки в PHP
Отправлено: Voldemar от 15 Декабря 2004, 21:55:36
уфф решил проблему. причем весьма неожиданно - заменил
$this->root = new dir(0, 0,\'Root\',$temp); // read root dir


на
$this->root =& new dir(0, 0,\'Root\',$temp); // read root dir