3

Does anyone here know when PHP executes include and include_once calls?

I'm wondering because I come from a Flash and Desktop development background where you have to import your classes at the beginning of each class.

But now that I am starting to build more complex PHP code, it seems like it would be better to just include classes when I actually need them to save on loading time. For example, look at the following psuedo code:

if (I_need_to_check_login)
 { 
   include_once "Class.class.php";
   $C = new Class();
 }

If I do this, is the file Class.class.php going to be included everytime the code is run or only when I execute the include_once.

I'm a bit of a Class freak and usually build a class for just about any functionality used by my apps, so I often have lots of class files.

1
  • 2
    You may want to look into an autoloader strategy. It's more efficient than using include_once/require_once and simplifies your code Commented Aug 26, 2011 at 1:14

4 Answers 4

5

include, include_once are standard PHP instructions. This means the interpreter executes each include, include_once when he finds one in the flow of the program. If a control structure avoids to execute a piece of code which has an include instruction, this one won't be executed.

In your example, the include_once, will be executed if, and only if, I_need_to_check_login is true.

Sign up to request clarification or add additional context in comments.

Comments

4

Includes are only performed if interpreter ever gets there. eg:

$variable = false;
if ($variable) {
    include 'a.php';
} else {
    include 'b.php';
}

In this case only b.php would be included.

For more on its behavior: PHP Manual - include

4 Comments

Sorry, based on what section of the manual page is it stated that the code will be included conditionally at runtime? It certainly get executed conditionally, but as I stated below in my comment, it can easily be proven that php parses files that aren't executed.
@gview No, and you are demonstrably wrong. See this and this; it's the same script - but you get to pick which file to include. In the first case everything works fine. In the 2nd case, it tries to include a file that has a syntax error in it, and fails upon including it - entirely contradicting what you said in your comment.
Yes, I was wrong on this -- my test case had a silly error in it. The manual page does not contain anything about this that I could find however, but I understand that is not why you included a link to it.
@gview I believe it's somewhere in the manual but I can't find it right now.
4

Yes, every time you have to call: $C = new Class(); you will have to require or include the file. Now what you can do instead of include_once is:

if (!class_exists('MyClass')) {
    include("Class.class.php");
}

which mean you might not have you include it again, if you already included it before. This will also improve performance since include is faster to execute than include_once.

2 Comments

what's wrong with using include_once with class_exists check?
class_exists will check if the class is already loaded (for example you already included it in other files) - now include_once will have to check your code if he already included the file. Read stackoverflow.com/questions/4326435/… for more info. (I recommend to run an STRACE to see how it's working).
2

Files are actually included only if you perform include_once command.

So if that I_need_to_check_login evaluates to false - file will not be included at all

It can be easily checked by yourself with adding echo "I've been included"; exit; to the first line of that file.

5 Comments

include_once simply does a check to see if the target file has already been included. Otherwise it will simply load the source again, and depending on the contents this could be a problem.
@gview: correct, but what did you try to say to me? The question I answered is "If I do this, is the file Class.class.php going to be included everytime the code is run or only when I execute the include_once." and the short answer is "it runs only when include_once is executed". Cannot get how it is related to your notice.
what I'm saying is that the way I read your response, I believe your answer is incorrect. There are many things that can be included but not executed. If i have an include file that declares a bunch of functions that aren't used, they are included, but not executed.
@gview: you're cheating - included file has been parsed anyway. you're using executed term in confusing way. Again, please read the original question: "If I do this, is the file Class.class.php going to be included everytime the code is run or only when I execute the include_once."
@zerk He/she is confused about how include works in PHP. See his/her answer and comments on this question and its answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.