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.

Antony
  • 14,900
  • 10
  • 46
  • 74
Lee Loftiss
  • 3,035
  • 7
  • 45
  • 73
  • 2
    You may want to look into an [autoloader strategy](http://php.net/manual/en/language.oop5.autoload.php). It's more efficient than using `include_once`/`require_once` and simplifies your code – Phil Aug 26 '11 at 01:14

4 Answers4

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.

András Gyömrey
  • 1,770
  • 1
  • 15
  • 36
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

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 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 Aug 26 '11 at 01:53
  • @gview No, and you are demonstrably wrong. See [this](http://left4churr.com/demos/inc_test.php) and [this](http://left4churr.com/demos/inc_test.php?b); 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. – NullUserException Aug 26 '11 at 02:13
  • 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 Aug 26 '11 at 02:21
  • @gview I believe it's *somewhere* in the manual but I can't find it right now. – NullUserException Aug 26 '11 at 02:23
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.

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • what's wrong with using `include_once` with `class_exists` check? – zerkms Aug 26 '11 at 01:15
  • 1
    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 http://stackoverflow.com/questions/4326435/php-include-vs-include-once-speed for more info. (I recommend to run an STRACE to see how it's working). – Book Of Zeus Aug 26 '11 at 01:20
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.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    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 Aug 26 '11 at 01:15
  • 1
    @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. – zerkms Aug 26 '11 at 01:16
  • 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 Aug 26 '11 at 01:55
  • @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." – zerkms Aug 26 '11 at 02:09
  • @zerk He/she is confused about how `include` works in PHP. See his/her answer and comments on this question and its answers. – NullUserException Aug 26 '11 at 02:17