Xu Wei Jie Posted March 6, 2009 Share Posted March 6, 2009 Hi all, I need to perform something like a goto statement. Every time, I run my script. It goes through a initialization routine. At some point of the code execution, I wish to redirect to that routine. Any idea how? This is the routine at the very start of the code /* main initialization routine */ $values = array(); $values_temp = array(); $adapt_inserter = new Inserter(); if (count($argv) > 1) { $id = fetch_data($argv[1]); } else { system('rm -r src tmp > /dev/null 2>&1'); if (!is_dir("tmp")) mkdir('tmp'); $id = 0; } /* end of initialization */ I tried to put this code inside a function and every time I want to reinitialize, I call that function. I placed this function at the beginning of the code but it does not work at all because I think its variables are not seen by the other self defined functions written after this particular routine. This is the function function reinitialize(){ global $values,$values_temp,$adapt_inserter,$id; /* main initialization routine */ $values = array(); $values_temp = array(); $adapt_inserter = new Inserter(); if (count($argv) > 1) { $id = fetch_data($argv[1]); } else { system('rm -r src tmp > /dev/null 2>&1'); if (!is_dir("tmp")) mkdir('tmp'); $id = 0; } } i.e. //This section of code is placed at the start of the script $values = array(); $values_temp = array(); $adapt_inserter = new Inserter(); reinitialize(); Any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/148182-solved-reinitializing-and-jumping-to-a-routine/ Share on other sites More sharing options...
vicodin Posted March 6, 2009 Share Posted March 6, 2009 So you are trying to pass variables between functions, im not 100% sure what your trying to accomplish. If so then look into Object Orientated PHP. You can create a class and you can pass and set variables inside your class using the $this-> statement. Another solution is to use a database and retireve those variables. Link to comment https://forums.phpfreaks.com/topic/148182-solved-reinitializing-and-jumping-to-a-routine/#findComment-777870 Share on other sites More sharing options...
Xu Wei Jie Posted March 6, 2009 Author Share Posted March 6, 2009 I am not trying to pass variables between functions but I am trying to do something like a goto statement. a: //Let my initialization routine be here //I run my code halfway and i goto here : a Thus I wrote a function that contains the initialization routine instead of codes where I can invoke the function anywhere I want to(i.e. which I want to invoke it at the start of the script). However these variables within the should also be seen by the other other self defined functions as well. Using an object will just complicate matters because I need to deal with creation and destroyal of the object. Any help would be appreciated =) Link to comment https://forums.phpfreaks.com/topic/148182-solved-reinitializing-and-jumping-to-a-routine/#findComment-777892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.