Jak załączyć () wszystkie pliki PHP z katalogu?

Bardzo szybkie pytanie n00b, w PHP mogę dołączyć katalog skryptów.

Czyli zamiast:

include('classes/Class1.php');
include('classes/Class2.php');

Czy jest coś takiego:

include('classes/*');

Nie mogłem znaleźć dobrego sposobu na włączenie kolekcji około 10 podklas dla danej klasy.

 205
Author: Rytmis, 2009-03-01

12 answers

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
 382
Author: Karsten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-01 11:44:39

Oto sposób, w jaki dołączam wiele klas z kilku folderów w PHP 5. To zadziała tylko, jeśli masz zajęcia.

/*Directories that contain classes*/
$classesDir = array (
    ROOT_DIR.'classes/',
    ROOT_DIR.'firephp/',
    ROOT_DIR.'includes/'
);
function __autoload($class_name) {
    global $classesDir;
    foreach ($classesDir as $directory) {
        if (file_exists($directory . $class_name . '.php')) {
            require_once ($directory . $class_name . '.php');
            return;
        }
    }
}
 50
Author: Marius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-04 14:39:16

Zdaję sobie sprawę, że to jest starszy post, ale... NIE WLICZAJ ZAJĘĆ... zamiast tego użyj _ _ autoload

function __autoload($class_name) {
    require_once('classes/'.$class_name.'.class.php');
}

$user = new User();

Wtedy za każdym razem, gdy wywołasz nową klasę, która nie została jeszcze włączona, php uruchomi automatycznie _ _ autoload i uwzględni ją dla ciebie

 27
Author: Banning,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-19 13:53:34

To tylko modyfikacja kodu Karstena

function include_all_php($folder){
    foreach (glob("{$folder}/*.php") as $filename)
    {
        include $filename;
    }
}

include_all_php("my_classes");
 19
Author: foobar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-13 21:43:26

Jeśli używasz php 5, możesz zamiast tego użyć autoload.

 18
Author: SorinV,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-01 11:44:25

Jak to zrobić w 2017:

spl_autoload_register( function ($class_name) {
    $CLASSES_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR;  // or whatever your directory is
    $file = $CLASSES_DIR . $class_name . '.php';
    if( file_exists( $file ) ) include $file;  // only include if file exists, otherwise we might enter some conflicts with other pieces of code which are also using the spl_autoload_register function
} );

Zalecane przez dokumentację PHP tutaj: klasy Autoloadingu

 14
Author: Sorin C,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-08 15:27:19

Możesz użyć set_include_path :

set_include_path('classes/');

Http://php.net/manual/en/function.set-include-path.php

 9
Author: albanx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-30 08:51:31

Jeśli chcesz umieścić wszystkie w katalogu i jego podkatalogach:

$dir = "classes/";
$dh  = opendir($dir);
$dir_list = array($dir);
while (false !== ($filename = readdir($dh))) {
    if($filename!="."&&$filename!=".."&&is_dir($dir.$filename))
        array_push($dir_list, $dir.$filename."/");
}
foreach ($dir_list as $dir) {
    foreach (glob($dir."*.php") as $filename)
        require_once $filename;
}

Nie zapominaj, że pliki będą zawierać się w porządku alfabetycznym.

 1
Author: Matthieu Chavigny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-22 08:43:12

Jeśli pomiędzy Plikami nie ma żadnych zależności ... oto funkcja rekurencyjna do include_once wszystkich plików php we wszystkich subdirach:

$paths = array();

function include_recursive( $path, $debug=false){
  foreach( glob( "$path/*") as $filename){        
    if( strpos( $filename, '.php') !== FALSE){ 
       # php files:
       include_once $filename;
       if( $debug) echo "<!-- included: $filename -->\n";
    } else { # dirs
       $paths[] = $filename; 
    }
  }
  # Time to process the dirs:
  for( $i=count($paths)-1; $i>0; $i--){
    $path = $paths[$i];
    unset( $paths[$i]);
    include_recursive( $path);
  }
}

include_recursive( "tree_to_include");
# or... to view debug in page source:
include_recursive( "tree_to_include", 'debug');
 1
Author: Sergio Abreu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-19 12:58:11

Sugeruję użycie funkcji readdir () , a następnie zapętlenie i dołączenie plików(zobacz pierwszy przykład na tej stronie).

 0
Author: Stiropor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-01 11:42:55

Jeśli chcesz dołączyć kilka klas bez konieczności definiowania każdej klasy na raz, możesz użyć:

$directories = array(
            'system/',
            'system/db/',
            'system/common/'
);
foreach ($directories as $directory) {
    foreach(glob($directory . "*.php") as $class) {
        include_once $class;
    }
}

W ten sposób możesz po prostu zdefiniować klasę w pliku php zawierającym klasę, A nie całą listę $thisclass = new thisclass();

Jak dobrze obsługuje wszystkie pliki? Nie jestem pewien, czy może to spowodować nieznaczne zmniejszenie prędkości.

 0
Author: Scott Dawson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-26 19:18:50

Nie zapisuje funkcji do dołączania plików do katalogu. Możesz stracić zakresy zmiennych i być może będziesz musiał użyć "globalnego". Zapętlaj akta.

Możesz również napotkać trudności, gdy dołączony plik ma nazwę klasy, która rozszerzy się na inną klasę zdefiniowaną w innym pliku - która nie jest jeszcze dołączona. Więc bądź ostrożny.

 -2
Author: bimal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 08:05:17