Jump to content

Does code imported with require or include stay indefinitely?


DWilliams

Recommended Posts

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?

Link to comment
Share on other sites

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.

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.