Jump to content

variables for ONLY this instance of a method?


linus72982

Recommended Posts

So, I have what I think is an odd question.  I have a function that contains a for loop.  Within that for loop, it checks for a new condition and if it is true, it calls the very function it's already in.  So, within the for loop inside of function "a", it can call function "a" with new arguments which will then start a new for loop to be "nested" to the previous for loop, in a way.  My problem is trying to keep the values of the variables so that once the new for loop (or loops) finish, the previous for loop won't be stopped up by the new variable values that have since changed.  Make sense?  So, two questions:

 

1. How do I lock in values that make them unique to that very call of that function and keep them unique through further iterated versions?

 

2. I imagine, in theory, that for loops stay in memory in state as a new "instance" of that same for loop runs through, right?  Basically, is what I'm doing possible in that a for loop calls itself, in essence, to create a new "instance" of that for loop, then when it finishes, have the previous loop continue iterating?

 

For question #1, I've already thought about using variable variables, but that doesn't work because every time the function is called, the variable variable will change to new values based on the new arguments and instantiating the function again.  I'm kind of looking for a way to make unique "instances" of a method that don't mess with future calls to that method and where future calls don't mess with currently iterating loops in that method.

 

Phew, I think I confused myself.  Thanks for any help.

Link to comment
Share on other sites

Ok this is a "Recursive Function".

 

You design your loop so that everytime it is called it does the same thing, for every element or any data its acting on. Ideally, at the end, you get the modified data you wanted, in a single variable, but something this is not exactly what you want.

 

You should never really need that "parent instance" check, so I would reccomend you post the appropriate code needed to know what your trying to do.

Anyway your requested solution would be to:

 

Set a GLOBAL VARIABLE (Before you run you loop) (this can be a CLASS PROPERTY if the method your talking about is in a class.).

 

The first thing the loop does is check if the GLOBAL VARIABLE has a certain value. If it has TRUE (for example), it is the first run, therefor, change it to FALSE, and run the "first loop" code.

Otherwise, run the "recursive loop" code.

 

GLOBAL $firstrun = TRUE;

function test($x){

// So the loop actually ends..
if($x >= 100){
	break;
} //


if($firstrun == TRUE){
	echo "FIRST RUN $x<br />";

	$x++;
	return test($x);
}else{
	echo "NEXT RUN $x<br />";

	$x++;
	return test($x);
}
}

echo(" <br /> Last Run: ".test(1);

Link to comment
Share on other sites

That would work for two tiers worth of loops, but this can possibly have 5, 10, 20 even.  This is a script to find replies and then it calls itself to find replies of replies, and calls again to find replies of replies of replies, etc.  Once it exhausts each reply tier, it should continue the previously started for loops (iterating x times where x is the amount of numReplies from the database) to find subsequent replies and then it will find replies of those replies, that sort of thing.  The problem I'm having is that all the values I want to identify a specific for loop keep changing because I'm calling the same function over and over.  I'm thinking what my answer here might be is to somehow count the number of "tiers" of replies down I go and back up a counter every time a for loop ends.  Then I can have all of my useful variables be arrays with the pointer as the keys, that way I'll have unique variables for each "instance" of the function?  Gah, it's very complicated and I'm having a hard time organizing it in my head.  I'll post the actual script when I get home to see if we can't work something out.  Thanks for all the help.

 

-Adam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.