creating a simple class isn't simple...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
creativetim
Forum Newbie
Posts: 4
Joined: Thu Sep 28, 2006 4:46 pm

creating a simple class isn't simple...

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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

Code: Select all

$this->$assetID // et al
to

Code: Select all

$node->assetID
Now, so PHP has $node, add

Code: Select all

$node = new Node();
before $node->assetID.

Finally, to access the property:

Code: Select all

$nodeList[1]->assetID
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to take a look at the Composite pattern.
(#10850)
creativetim
Forum Newbie
Posts: 4
Joined: Thu Sep 28, 2006 4:46 pm

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
creativetim
Forum Newbie
Posts: 4
Joined: Thu Sep 28, 2006 4:46 pm

Post 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!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
creativetim
Forum Newbie
Posts: 4
Joined: Thu Sep 28, 2006 4:46 pm

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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 />";
}
Post Reply