Variable eller referencer?
Hej,Jeg er i gang med at lave en grundstruktur til en hjemmeside, og har lidt problemer med at finde ud af hvordan jeg skal håndtere objekter. Primært er der to class'er: tree og node, de kommer her i en kort udgave:
TREE............................................................
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
class Tree {
var $start; //Reference to the first element
var $cursor; //Reference to the current element
//Constructor
//Pre: Nothing
//Post: The tree is loaded, $this->start and $this->cursor contains a references to the first element.
// If the tree is loaded correctly, the function returns true, otherwise it returns false
function Tree()
{
$temp_start = $this->LoadTree("");
if(is_object($temp_start))
{
$bool1 = $this->SetStart($temp_start);
$bool2 = $this->GoToFirst();
if($bool1 && $bool2)
{
return true;
}
else
{
echo "class_tree.inc - Tree: Something went wrong, SetStart: $bool1, GoToFirst: $bool2.";
return false;
}
}
else
{
return false;
}
}
//Function LoadTree
//Pre: A node is created and set as reference to this function
//Post: The subtree is created and the first element in the sequense is returned.
function LoadTree($NodeObj)
{
if(is_object($NodeObj))
{
$temp_nodeid = $NodeObj->GetNodeId();
}
else
{
$temp_nodeid = 0;
}
$first = NULL;
$temp_cursor = NULL;
$SQL = "SELECT NodeId FROM Node WHERE ParentNode='" . $temp_nodeid . "' ORDER BY NodePriority";
$RS = sql_forespoerg($SQL);
for($j=0; $j<sizeof($RS); $j++)
{
extract($RS[$j]);
//Create new page
$temp_node = new Node($NodeId);
//Load the submenu for this new item...
$temp_node->SetChild($this->LoadTree($temp_node));
if($j==0)
{
//... and assign to the variable first (only first object)
$first = $temp_node;
$temp_cursor = $temp_node;
}
else
{
//... or assign to the end of the row of pages
$temp_cursor->SetNext($temp_node);
//Move to the new item
$temp_cursor = $temp_cursor->GetNext();
}
}
//Returns the first item, and NULL if there isn't any item
return $first;
}
}
NODE............................................................
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
class Node {
var $nodeid;
var $level;
var $nextNode;
var $previousNode;
var $childNode;
var $parentNode;
var $content;
//Constructor
function Node($id)
{
$this->SetNodeId($id);
//Loading content into Node
$this->content = Array();
$SQL = "SELECT NodeInstanceId FROM NodeInstance WHERE NodeId = '" . $this->GetNodeId() . "' ORDER BY LanguageId";
$RS = sql_forespoerg($SQL);
for($i=0; $i<sizeof($RS);$i++)
{
extract($RS[$i]);
$this->SetContent(new NodeInstance($NodeInstanceId));
}
//Saving the parent-info into the pageobject
$SQL = "SELECT ParentNode FROM Node WHERE NodeId = '" . $this->GetNodeId() . "'";
$RS = sql_forespoerg($SQL);
extract($RS[0]);
$this->SetParent($ParentNode);
//Setting Level
$this->SetLevel();
}
//////////////////////////////////////////////////////////////////////////////
// SET FUNCTIONS //
//////////////////////////////////////////////////////////////////////////////
function SetNodeId($var)
{
$this->nodeid = $var;
}
function SetNext(&$NodeObj)
{
$this->nextNode = $NodeObj; //Der skal muligvis tilføjes mere til denne funktion?!
}
function SetPrevious(&$NodeObj)
{
$this->previousNode = $NodeObj; //Der skal muligvis tilføjes mere til denne funktion?!
}
function SetChild(&$NodeObj)
{
if($NodeObj != "")
{
//If this already has a child, then this is put as 'next' to new child
if(is_object($this->GetChild()))
{
$NodeObj->SetNext($this->GetChild());
}
$this->childNode = $NodeObj; //Assign NodeObj to this->child
$this->childNode->SetParent($this); //Set parent for the new child
$this->childNode->SetLevel(); //Set level for the new child
return true;
}
else
{
return false;
}
}
function SetParent(&$NodeObj)
{
$this->parentNode = $NodeObj;
}
//////////////////////////////////////////////////////////////////////////////
// GET FUNCTIONS //
//////////////////////////////////////////////////////////////////////////////
function GetNodeId()
{
return $this->nodeid;
}
function GetNext()
{
return $this->nextNode;
}
function GetPrevious()
{
return $this->previousNode;
}
function GetChild()
{
return $this->childNode;
}
}
Der hvor jeg har brug for hjælp er: når jeg opretter et tree object, så loader den alt in fra db, men mit tree består kun af den første node. Det lader til, at der er nogle problemer med mine referencer - er lidt i tvivl om jeg skal gøre følgende:
$temp_cursor = $this->GetCursor()
eller...
$temp_cursor = &$this->GetCursor()
Dette vil jeg gerne have hjælp til + alt andet der har relevans. Jeg hæver gerne pointantallet hvis dette ønskes.
Mvh, Steven
www.mountfield.dk
