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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.