DWilliams Posted June 2, 2010 Share Posted June 2, 2010 Let's say I had a program that was going to interpret a user's text input. If I wanted to make it modular and extensible I could just have a "commands" directory with one file containing php code for each command. If in my main script I had a while loop running indefinitely that would loop once for every user input and process it, could I do something like the following? while(!$shutdown) { if(isset($command) require_once("commands/$command.php"); } In theory, if $command gets set to something like 'sayhi', the script would import sayhi.php and execute the code in that file (code is just for demonstrating the idea, I know it has security problems). Now, what happens the next time the loop loops? Will it clean up after itself and properly get rid of the last commands variables/statements and be ready for the next one or will things go bad quickly? Would it be better to read the file into a variable and eval() it, or better yet read every command's code into an array at program start to save disk access time? Quote Link to comment https://forums.phpfreaks.com/topic/203587-does-code-imported-with-require-or-include-stay-indefinitely/ Share on other sites More sharing options...
Alex Posted June 2, 2010 Share Posted June 2, 2010 or better yet read every command's code into an array at program start to save disk access time? This. Otherwise every time you entered the command it would have to re-include the file, this isn't necessary. Quote Link to comment https://forums.phpfreaks.com/topic/203587-does-code-imported-with-require-or-include-stay-indefinitely/#findComment-1066452 Share on other sites More sharing options...
Mchl Posted June 2, 2010 Share Posted June 2, 2010 Put your commands in functions and just call them when needed. Quote Link to comment https://forums.phpfreaks.com/topic/203587-does-code-imported-with-require-or-include-stay-indefinitely/#findComment-1066549 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.