Current location: Hot Scripts Forums » Programming Languages » PHP » Reassign a variable, linked to a class, from within the same class


Reassign a variable, linked to a class, from within the same class

Reply
  #1 (permalink)  
Old 04-27-09, 05:21 PM
Ryan75 Ryan75 is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Unhappy Reassign a variable, linked to a class, from within the same class

I have a class that is setup similar to a pointer system (head, node, tail) of the same class. The class is first connected to a variable in the main php script
ex. $master = new Node();

to create new nodes which attach the tail/head properly are like such.
$master->addNode("value1");$master->addNode("value2");$master->addNode("value3");

If I print out the nodes, starting at the master I get as such:
<1> <2> <3> . . .
where <1> is the master node

All the assigning, moving, deleting works great as it reconnects a string of nodes created from the master Node.

The main, only, problem I've run into is reassigning a NEW first node to the variable.

I'm looking for a way to either access the variable assigned to the class, or even pass the same variable to the class while it's assigning.

Anyone know how to handle this or what commands to look at? Isn't there a command to find the variable parent to the class, just as there is a function to find what class is the parent of the subclass?

Thanks. (hope that was all clear)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-27-09, 06:23 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
Classic linked list problem.
http://en.wikipedia.org/wiki/Linked_list

Google on linked lists...

Either you can have single or double linked nodes.

With single linked nodes, you have to start at the head and traverse
the list until you've found the node under investigation.

With double linked nodes, you can traverse the list in either direction.

Benefits:
Single linked lists conserve memory
Double linked lists are more efficient for searching.

As far as inserting a node, you have to store the pointers of the two nodes
at the insertion point, update the to be inserted node with these pointers,
and change the pointers of original nodes to point to the newly inserted node.

When adding a node at the top of the list, you only have to deal with parent pointer
of the top node; which should NULL or false at that point.

Additionally, you won't be able to just use an "in-order"
traversal scheme (I mean the order that the nodes were inserted),
to traverse the list. You'll need methods to use the pointers to
follow the links.

But it sounds like you've already set that up.

Ergo, you just need to modify your "insertNode" method.

i.e.
- node = createNewNode();
- list->insertNode(0,node);

Last edited by dgreenhouse; 04-27-09 at 06:34 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-28-09, 12:30 AM
Ryan75 Ryan75 is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the comment and clarification with pointers dgreenhouse. I do have the linked list system created though with the double links. silly me forgot about single links which in my case, that will be better possibly since when done, I will execute from top-bottom with no need to look back.

The reassigning of the parent pointer is the problem...

Since the parent is a variable. I couldn't find anyway to adjust the parent variable to point to a new node.

The ONLY temporary solution I have is using the $GLOBALS part of php to assign the variable again. What I currently do now is when a linked list is created, I store the name of the variable that it will be under from within the class like such..

$master = new Node('master'); <--- the name passed is stored in in the node, and every additional node inherits that name into it's storage.

Then when I adjust the top node, I do similar to the below:

if ($somenode->head == null ) $GLOBALS[$this->varname] = $somenode;


Do you know of a better solution that using the GLOBALS ??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading XML anupamsr PHP 13 04-19-08 01:57 PM
object manager class / object class ther PHP 0 07-14-07 01:36 PM
conceptual problem: abstract class, interface or class UnrealEd Everything Java 2 05-12-07 08:30 AM
I need to pass this class Please Help! Negative6 Everything Java 1 05-03-07 07:55 AM
using variable variables with multiple variables for parts of the name harlock PHP 7 07-26-05 08:35 PM


All times are GMT -5. The time now is 01:41 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.