Returning reference problem

From: Date: Sat, 29 Jan 2005 09:29:08 +0000
Subject: Returning reference problem
Groups: php.general 
Request: Send a blank email to php-general+get-207675@lists.php.net to get a copy of this message
Starting over since my last thread had too little effort put in it. I have this script that should create a multi-demensional array that would resemble a directory structure. The problem is that after adding one element to a "folder" it appears to be creating a new object instead of adding another item to the current object. So there can only be one "file" or "folder" in the parent "folder" with the exception of the top level. My entire script is below. Thanks, Jon <? class dir { var $name; var $subdirs; var $files; var $num; var $prio; function dir($name,$num,$prio) { $this->name = $name; $this->num = $num; $this->prio = $prio; $this->files = array(); $this->subdirs = array(); } function addFile($file) { $this->files[] =& $file; return $file; } function &addDir($dir) { $this->subdirs[] =& $dir; return $dir; } function &findDir($name) { foreach($this->subdirs as $v){ if($v->name == $name) return $v; } return false; } function draw($parent) { echo('d.add('.$this->num.','.$parent.',"'.$this->name."\",".$this->prio.");\n"); foreach($this->subdirs as $v) { $v->draw($this->num); } foreach($this->files as $v) if(is_object($v)) { echo("d.add(".$v->num.",".$this->num.", \"".$v->name."\",".$v->prio.");\n"); } } } class file { var $name; var $prio; var $size; var $num; function file($name,$num,$size,$prio) { $this->name = $name; $this->num = $num; $this->size = $size; $this->prio = $prio; } } $arFiles = array ( 0 => array ( 'path' => array ( 0 => 'folder1', 1 => 'subfolder1', 2 => 'file1.ext' ), 'length' => 5464, 'size' => 8765 ), 1 => array ( 'path' => array ( 0 => 'folder1', 1 => 'subfolder1', 2 => 'file2.ext' ), 'length' => 5464, 'size' => 8765 ), 2 => array ( 'path' => array ( 0 => 'folder1', 1 => 'subfolder2', 2 => 'file1.ext' ), 'length' => 5464, 'size' => 8765 ), 3 => array ( 'path' => array ( 0 => 'folder2', 1 => 'subfolder1', 2 => 'file1.ext' ), 'length' => 5464, 'size' => 8765 ) ); $prio = array(); for($i=0;$i<count($arFiles);$i++) $prio[$i] = -1; $dirnum = count($arFiles); $tree = new dir("/",$dirnum,isset($prio[$dirnum])?$prio[$dirnum]:-1); foreach( $arFiles as $filenum => $file) { $depth = count($file['path']); $branch =& $tree; for($i=0; $i < $depth; $i++){ if ($i != $depth-1){ $d =& $branch->findDir($file['path'][$i]); echo "<BR><B>Tree after find dir:</B>"; print_r($tree); if($d) $branch =& $d; else{ $dirnum++; $d =& $branch->addDir(new dir($file['path'][$i], $dirnum, (isset($prio[$dirnum])?$prio[$dirnum]:-1))); echo "<BR><B>Tree after add dir:</B>"; print_r($tree); $branch =& $d; } }else{ $branch->addFile(new file($file['path'][$i]." (".$file['length'].")",$filenum,$file['size'], $prio[$filenum])); echo "<BR><B>Tree after add file:</B>"; print_r($tree);} } } $tree->draw(-1); ?>

« previous php.general (#207675) next »