Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 13
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Prawdziwa skarbnica wiedzy na temat tworzenia stron WWW i nie tylko. Korzystam z porad praktycznie codziennie, jest mi to niezbędne w mojej pracy. Sam zajmuję się tworzeniem serwisów, ale porady pisane przez Darka sa dla mnie nieocenioną pomocą! Proste, czytelne i zrozumiałe dla każdego! Czekam na więcej!

Krzysztof Szypulski
KESS - projektowanie stron

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

require_once()

Instrukcja require_once() służy do wczytania i wykonania kodu z określonego pliku w trakcie wykonywania skryptu. Zachowanie jej jest identyczne z instrukcją require() z jednym wyjątkiem, tzn. jeśli dany plik został już raz wczytany do tego skryptu, nie będzie wczytany ponownie. Więcej informacji na temat działania tej instrukcji w dokumentacji odnośnie require().

require_once() powinno być używane w przypadku, gdy ten sam plik mógłby zostać wczytany i wykonany wielokrotnie, ale ty chcesz mieć pewność, że zostanie wczytany dokładnie raz, aby uniknąć problemów z redefiniowaniem funkcji, nadpisywaniem zmiennych itp.

Przykłady użycia require_once() i include_once() znajdują się w kodzie PEAR dołączonym do najnowszych dystrubucji kodu źródłowego PHP.

Notatka: require_once() dodano w PHP 4.0.1pl2

Notatka: Proszę pamiętać, że require_once() oraz include_once() mogą zachowywać się w sposób nieoczekiwany na systemach operacyjnych nierozróżniających wielkości liter (non case-sensitive) - takich jak Windows.

Przykład 16-8. require_once() zwraca uwagę na wielkość liter

require_once("a.php"); // to wczyta plik a.php
  require_once("A.php"); // na Windows to ponownie wczyta a.php !

Ostrzeżenie

PHP w wersji starszej niż 4.3.0, pracujące pod kontrolą systemów Windows, nie obsługują dostępu do zdalnych plików w tej funkcji, nawet jeśli opcja allow_url_fopen jest włączona.

Patrz także: require(), include(), include_once(), get_required_files(), get_included_files(), readfile(), i virtual().




User Contributed Notes

sdh00b at gmail dot com
05-Dec-2005 04:12

@georg_gruber at yahoo dot com

Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.

Taken from http://us2.php.net/manual/en/function.include.php


georg_gruber at yahoo dot com
07-Oct-2005 12:06

A very interesting behaviour of require_once (and probably all include commands):

consider the following files:

/index.php -> require_once('/inc/library.php');
/function1.php -> print('/function1.php');
/inc/library.php -> require_once('function1.php');
/inc/function1.php -> print('/inc/function1.php');

Note that /function1.php and /inc/function1.php are files with the SAME filename in different folders.

If you "/index.php" is executed it will output
"/function1.php".

Although /index.php "includes" /inc/library.php the scope of the file is still /index.php therefor /function1.php will be found even it could be asumed the /inc/function1.php is the correct one.

And it gets more interesting: if you delete /function1.php and execute /index.php PHP checks this and "includes" /inc/function1.php.


miqrogroove
01-May-2005 08:10

require_once() is NOT independent of require().  Therefore, the following code will work as expected:

echo.php
<?php
echo "Hello";
?>

test.php
<?php
require('echo.php');
require_once(
'echo.php');
?>

test.php outputs: "Hello".

Enjoy,
-- Miqro


ulderico at maber dot com dot br
22-Mar-2005 02:38

With both of your functions guys, Pure-PHP and jtaal at eljakim dot nl, you'll not have any variables available GLOBALly if they're supposed to be globals...

That's why my import handles better those situation. OK, SOME MAY DISPUTE that using include_once and require_once may slow down an application. But what's the use to do IN PHP what the interpreter *should* do better for you. Thusly these workarounds shall, some time in the future, DIE.

Thus It's better to well design your application to keep some order using few INCLUDES and REQUIRES in it rather than insert MANY AND SEVERAL *_once around.


Pure-PHP
17-Mar-2005 11:19

require_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

require_once("includeWrapper.class.php")

includeWrapper::require_once("Class1.class.php");
includeWrapper::require_once("Class1.class.php");
includeWrapper::require_once("Class2.class.php")


jtaal at eljakim dot nl
10-Mar-2005 02:00

When you feel the need for a require_once_wildcard function, here's the solution:

<?php // /var/www/app/system/include.inc.php

function require_once_wildcard($wildcard, $__FILE__) {
 
preg_match("/^(.+)\/[^\/]+$/", $__FILE__, $matches);
 
$ls = `ls $matches[1]/$wildcard`;
 
$ls = explode("\n", $ls);
 
array_pop($ls); // remove empty line ls always prints
 
foreach ($ls as $inc) {
   require_once(
$inc);
  }
}

?>

The $__FILE__ variable should be filled with the special PHP construct __FILE__:
<?php // /var/www/app/classes.inc.php

require_once('system/include.inc.php');
require_once_wildcard("classes/*.inc.php", __FILE__);

?>

The (*.inc.php) files inside the directory classes are automagically included using require_once_wildcard.

This solution may not be as useful when using PHP5 in combination with classes and the autoload feature.

--
Jaap Taal


thomas dot revell at uwe dot ac dot uk
21-Jan-2005 11:00

Regarding the case insensitivity problems on Windows, it looks to me as though it is a problem in PHP5 as well (at least in some cases).

The following gave me problems:

From file URLSwitcher.php
<?php
require_once 'slimError/slimError.php';
require_once
'Navigator_Cache.php';
....
?>

From file Navigator_Cache.php
<?php
require_once 'slimError/slimerror.php';
...
?>

From file slimerror.php
<?php
class SLIMError {
...
}
?>
The above setup gave me an error : "Cannot redeclare class SLIMError"

If I change the require_once in URLSwitcher.php to match the one in Navigator_Cache.php, there isn't a problem, but if I do this the other way round, the same problem occurs.


18-Mar-2004 06:49

> Mac OS X systems are also not case-sensitive.
That depends on the filesystem:
- HFS and HFS+ are NOT case sensitive.
- UFS is case sensitive.


jaisen - at - jmathai - dot - com
12-Mar-2004 09:16

NOTE: This function changed how it worked.  In PHP 3 this behaved very differently than it does on PHP 4.  Require used to include and parse the file regardless where the require line was positioned.

For example (PHP3):

<?php
 
if(false){ require_once 'file_does_not_exist.php'; }
?>

That code throw a fatal exception even though it's in a conditional block which evaluates to false.  In PHP 4 the file is never included or parsed, so no exception is thrown.

For example (PHP4)
<?php
 
if(false){ require_once '1_file_does_not_exists.php'; }
  require_once
'2_file_does_not_exists.php';
?>

Stops execution of the script on trying to require the 2nd file...by bypasses the first require.

--JM


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt