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
include_once()
include_once() bindet eine angegebene Datei ein und führt sie als PHP-Skript aus. Dieses Verhalten ist identisch zu include(), mit dem einzigen Unterschied, dass die Datei, wenn sie bereits eingebunden wurde, nicht erneut eingebunden wird. Wie der Name schon sagt, wird sie nur einmal eingebunden werden.
include_once() kann beispielsweise benutzt werden, wenn die selbe Datei an mehreren Stellen eingebunden wird, um Problemen durch erneute Definitionen von Funktionen, erneute Zuweisung von Variablen oder ähnliches zu vermeiden.
Für die Funktionsweise der Funktion, siehe die Dokumentation zu include().
Hinweis:
Mit PHP 4 wird auf Betriebssystemen, die bei Dateinamen nicht zwischen Groß- und Kleinschreibung unterscheiden (wie Windows), bei _once dennoch zwischen Groß- und Kleinschreibung unterschieden, wie das folgende Beispiel zeigt:
Beispiel #1 include_once() mit einem case-insensitive Betriebssystem und PHP 4
<?php
include_once "a.php"; // a.php wird eingebunden.
include_once "A.php"; // a.php wird nochmal eingebunden! (Nur in PHP 4)
?>Dieses Verhalten hat sich in PHP 5 geändert, so dass zum Beispiel bei Windows der Pfad zuerst normalisiert wird, sodass C:\PROGRA~1\A.php und C:\Program Files\a.php als das selbe erkannt und nur einmal eingebunden werden.
include_once
10-Aug-2006 02:11
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
26-May-2005 04: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');
}
?>
17-Mar-2005 11: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")
29-Oct-2004 12: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.