Page 1 of 1
creating a simple class isn't simple...
Posted: Thu Sep 28, 2006 4:53 pm
by creativetim
This is my code:
Code: Select all
# Create array to hold all nodes
$nodeList = array();
# Create node class
class Node{
function CreateNode($assetID, $parent, $level, $children, $name, $URL){
$this->$assetID = $assetID;
$this->$parent = $parent;
$this->$level = $level;
$this->$children = $children;
$this->$name = $name;
$this->$URL = $URL;
}
}
# Populate the nodeList array
$myNode = new Node;
$nodeList[0] = $myNode->CreateNode(1,0,0,2,"Index","#");
$nodeList[1] = $myNode->CreateNode(2, 0, 1, 1, "Sub 1", "#");
$nodeList[2] = $myNode->CreateNode(3, 0, 2, 0, "Sub-Sub", "#");
$nodeList[3] = $myNode->CreateNode(4, 0, 1, 0, "Sub 2", "#");
echo $nodeList[1].assetID;
Hopefully, you can see what I'm trying to do. I just want to append to the $NodeList array and be able to retrieve data from it using $NodeList[1].property. When I run this code all I get is:
assetID
I'm sure this is something easy, but I'm like brand-new to PHP so I'm at a loss of where to go from here and Googling just isn't helping, it's only gotten me here so far.
Posted: Thu Sep 28, 2006 5:06 pm
by feyd
It would appear your code is designed for Javascript more than PHP.
CreateNode() doesn't return anything, so PHP gives each array element a null value. I'll guess that you want a new Node returned. Therefore change
to
Now, so PHP has $node, add
before $node->assetID.
Finally, to access the property:
Posted: Thu Sep 28, 2006 5:21 pm
by Christopher
You might want to take a look at the Composite pattern.
Posted: Thu Sep 28, 2006 8:11 pm
by creativetim
that didn't work. it seems weird to invoke a class in a function of the class you're attempting to invoke. or maybe i didn't understand. is it possible for you to make the changes to my code and post that so that there is no misunderstanding?
Posted: Thu Sep 28, 2006 8:59 pm
by Luke
anywhere you are considering using dot syntax ie: object.method() or object.property, use -> instead... it is the php equivalant of the dot. So you would do object->method() and object->property... or to be more specific, $nodeList[1].assetID; would be $nodeList[1]->assetID;
In php, the dot is used as a concatenator (to append one string to another)
Code: Select all
$string = "this is a string " . " and this is some more string";
echo $string; // outputs this is a string and this is some more string
Posted: Thu Sep 28, 2006 9:24 pm
by Ambush Commander
When you call createNode, you mean to create a new Node class. PHP requires that you actually instantiate the new node. In this case, you could have very well used Node::createNode(), which is a static function.
Your naming convention suggests you might want to investigate the
DOM extension.
Posted: Thu Sep 28, 2006 9:27 pm
by creativetim
a lot of this is over my head right now, but i got it to work be returning $this in the CreateNode function, as follows:
Code: Select all
# Create array to hold all nodes
$nodeList = array();
# Create node class
class Node{
function CreateNode($assetID, $parentNode, $level, $children, $name, $URL){
$this->assetID = $assetID;
$this->parentNode = $parentNode;
$this->level = $level;
$this->children = $children;
$this->name = $name;
$this->URL = $URL;
return $this;
}
}
# Populate the nodeList array
$node = new Node;
$nodeList[0] = $node->CreateNode(1,0,0,2,"Index","#");
$nodeList[1] = $node->CreateNode(2,0,1,1,"Sub 1","#");
$nodeList[2] = $node->CreateNode(3,0,2,0,"Sub-Sub","#");
$nodeList[3] = $node->CreateNode(4,0,1,0,"Sub 2","#");
for($a=0; $a < count($nodeList); $a++){
echo $nodeList[$a]->name . "<br />";
}
i'm not sure if this is the "proper" way to do it, but it works. thanks everyone!
Posted: Thu Sep 28, 2006 9:29 pm
by Ambush Commander
That does work in PHP 4, although it will confuse the heck out of a lot of people who expect $node to be an actual object, not a class definition. And PHP 5 refs will cause the code to fail fantastically.
Posted: Thu Sep 28, 2006 9:35 pm
by creativetim
i don't really know much about PHP and classes and stuff. this will work for now. if there is a better way to do this, please post the code and i will gladly use it and try and understand why it's better.
Posted: Fri Sep 29, 2006 2:25 am
by jmut
creativetim wrote:a lot of this is over my head right now, but i got it to work be returning $this in the CreateNode function, as follows:
Code: Select all
# Create array to hold all nodes
$nodeList = array();
# Create node class
class Node{
function CreateNode($assetID, $parentNode, $level, $children, $name, $URL){
$this->assetID = $assetID;
$this->parentNode = $parentNode;
$this->level = $level;
$this->children = $children;
$this->name = $name;
$this->URL = $URL;
return $this;
}
}
# Populate the nodeList array
$node = new Node;
$nodeList[0] = $node->CreateNode(1,0,0,2,"Index","#");
$nodeList[1] = $node->CreateNode(2,0,1,1,"Sub 1","#");
$nodeList[2] = $node->CreateNode(3,0,2,0,"Sub-Sub","#");
$nodeList[3] = $node->CreateNode(4,0,1,0,"Sub 2","#");
for($a=0; $a < count($nodeList); $a++){
echo $nodeList[$a]->name . "<br />";
}
i'm not sure if this is the "proper" way to do it, but it works. thanks everyone!
well how about just using constructor..
Code: Select all
# Create array to hold all nodes
$nodeList = array();
# Create node class
class Node{
function Node($assetID, $parentNode, $level, $children, $name, $URL){
$this->assetID = $assetID;
$this->parentNode = $parentNode;
$this->level = $level;
$this->children = $children;
$this->name = $name;
$this->URL = $URL;
}
}
# Populate the nodeList array
$nodeList[0] = new Node(1,0,0,2,"Index","#");
$nodeList[1] = new Node(2,0,1,1,"Sub 1","#");
$nodeList[2] = new Node(3,0,2,0,"Sub-Sub","#");
$nodeList[3] = new Node(4,0,1,0,"Sub 2","#");
for($a=0; $a < count($nodeList); $a++){
echo $nodeList[$a]->name . "<br />";
}