PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Funktionen> <Kontroll-Strukturen
Last updated: Fri, 14 Nov 2008

view this page in

include_once()

Die Anweisung include_once() schließt zur Ausführungszeit die angegebene Datei ein und wertet diese aus. Dies ist ähnlich der include()-Anweisung; mit dem Unterschied, dass einmal eingebundener Code nicht nochmals eingebunden wird. Wie der Name vermuten lässt, wird die Datei nur ein einziges Mal eingebunden.

include_once() sollten Sie in den Fällen benutzen, wenn die gleiche Datei in einem bestimmten Bereich mehrmals eingebunden und interpretiert werden soll und Sie sicher stellen wollen, dass diese Datei nur exakt einmal eingebunden wird, um Probleme mit Wiederholungen von Funktionsdefinitionen und Wertzuweisungen an Variablen zu vermeiden, usw.

Weitere Beispiele zu require_once() und include_once() können Sie dem » PEAR-Code entnehmen, welcher im aktuellsten PHP-Source-Code enthalten ist.

Der Rückgabewert entspricht dem von include(). Wurde die Datei bereits eingebunden wird TRUE zurückgegeben.

Hinweis: include_once() steht ab PHP 4.01pl2 zur Verfügung.

Hinweis: Beachten Sie, dass auf auf einem Betriebssystem, das nicht zwischen Groß- und Kleinschreibung unterschiedet (wie z.B. Windows) das Verhalten von include_once() und require_once() nicht unbedingt ihren Erwartungen entspricht.

Beispiel #1 include_once() unterscheidet nicht zwischen Groß-/Kleinschreibung unter Windows

<?php
include_once("a.php"); // bindet a.php ein
include_once("A.php"); // bindet a.php auf Windows nochmal ein! (nur PHP 4)
?>
Dieses Verhalten hat sich in PHP 5 geändert - der Pfad wird zuerst normalisiert so dass C:\PROGRA~1\A.php und C:\Program Files\a.php als gleich erkannt werden und so die Datei nur einmal eingebunden wird.

Warnung

PHP Versionen kleiner 4.3.0 für Windows, erlauben den Zugriff auf Remote-Dateien mit dieser Funktion nicht, selbst wenn allow_url_fopen aktiviert ist.

Siehe auch include(), require(), require_once(), get_required_files(), get_included_files(), readfile() und virtual().



Funktionen> <Kontroll-Strukturen
Last updated: Fri, 14 Nov 2008
 
add a note add a note User Contributed Notes
include_once
roach dot scott+spam at googlemail dot com
28-Jun-2008 12:22
If you include a file that does not exist with include_once, the return result will be false.

If you try to include that same file again with include_once the return value will be true.

Example:
<?php
var_dump
(include_once 'fakefile.ext'); // bool(false)
var_dump(include_once 'fakefile.ext'); // bool(true)
?>

This is because according to php the file was already included once (even though it does not exist).
emanuele at rogledi dot com
19-May-2008 12:40
For include_once a file in every paths of application we can do simply this

include_once($_SERVER["DOCUMENT_ROOT"] . "mypath/my2ndpath/myfile.php");
php at metagg dot com
08-Aug-2007 10:29
If you are like me and make heavy use of the __autoload magic function, always set include paths so you can just instantiate your class, and have multiple locations and name schemes for your custom libraries then you might be frustrated by simple parse errors being supressed when using @include_once('lib.php').

The solution I came up with was:

define('IN_PRODUCTION_ENV',FALSE);

function __autoload($class){

  $paths = array();
  $paths[] = "{$class}_lib.php";
  $paths[] = "{$class}_inc.php";
  $paths[] = "{$class}.php";

  if(IN_PRODUCTION_ENV){
 
    foreach($paths as &$path){
   
      if((@include_once $path) !== false){ return; }//if
   
    }//foreach
 
  }else{
 
    // we are not in a production environment so we want to see all errors...
    $include_paths = explode(PATH_SEPARATOR,get_include_path());
   
    foreach($include_paths as $include_path){
   
      // go through each of the different class names...
      foreach($paths as $path){
     
        // attach each class name to the include path...
        $include_file = $include_path.$path;
     
        if(file_exists($include_file)){
       
          if((include_once $include_file) !== false){ return; }//if
       
        }//if
   
      }//foreach
   
    }//foreach
   
  }//if/else
 
  trigger_error("{$class} was not found",E_USER_ERROR);
 
}//method

Now, just make sure you define IN_PRODUCTION_ENV to true or false to get either the slower (with all parse errors shown) or the faster (just suppress everything) autoloading. Hope this helps someone else since it was annoying just having blank screens show up when I had a simple parse error. Thanks to flobee at gmail dot com for providing me with the epiphany on why pages were showing up blank...-Metagg
webmaster AT domaene - kempten DOT de
10-Aug-2006 12:11
Since I like to reuse a lot of code it came handy to me to begin some sort of library that I stored in a subdir
e.g. "lib"

The only thing that bothered me for some time was that although everything worked all IDEs reported during editing
these useless warnings "file not found" when library files included other library files, since my path were given all relative to the corresponding document-root.

Here is a short workaround that makes that gone:

<?php
// Change to your path

if(strpos(__FILE__,'/lib/') != FALSE){
   
chdir("..");
}
include_once (
'./lib/other_lib.inc');
// ... or any other include[_once] / require[_once]
?>

just adjust the path and it will be fine - also for your IDE.

greetings
29-Aug-2005 08:52
Dealing with function redefinitions

include_once and require_once are very useful if you have a library of common functions.  If you try to override with - that is define - an identically named local function however, PHP will halt noting that it cannot redeclare functions.  You can allow for this by bracketing (within the include file):
function myUsefulFunc($arg1, $arg2) {
     ... }

with

if (!function_exists('myUsefulFunc')) {
function myUsefulFunc($arg1, $arg2) {
     ... }}

Top level functions (ie. those not defined within other functions or dependent on code running) in the local file are always parsed first, so http://php.net/function_exists within the included/required file is safe - it doesn't matter where the include statements are in the local code.

Csaba Gabor from Vienna
flobee at gmail dot com
26-May-2005 02:55
i already had a discussion with several people about "not shown errors"
error reporting and all others in php.ini set to: "show errors" to find problems:
the answer i finally found:
if you have an "@include..." instead of "include..." or "require..('somthing') in any place in your code
all following errors are not shown too!!!

so, this is actually a bad idea when developing because paser errors will be droped too:
<?php
if(!@include_once('./somthing') ) {
    echo
'can not include';
}
?>

solution:
<?php
if(!@file_exists('./somthing') ) {
    echo
'can not include';
} else {
   include(
'./something');
}
?>
Pure-PHP
17-Mar-2005 10:17
Inlude_once can slower your app, if you include to many files.

You cann use this wrapper class, it is faster than include_once

http://www.pure-php.de/node/19

include_once("includeWrapper.class.php")

includeWrapper::includeOnce("Class1.class.php");
includeWrapper::requireOnce("Class1.class.php");
includeWrapper::includeOnce("Class2.class.php")
bioster at peri dot csclub dot uwaterloo dot ca
28-Oct-2004 10:06
Something to be wary of:  When you use include_once and the data that you include falls out of scope, if you use include_once again later it will not include despite the fact that what you included is no longer available.

So you should be wary of using include_once inside functions.

Funktionen> <Kontroll-Strukturen
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites