매우 빠른 n00b 질문입니다. PHP에서는 스크립트 디렉토리를 포함 할 수 있습니까?디렉토리의 모든 PHP 파일을 포함시키는 방법()
include('classes/Class1.php');
include('classes/Class2.php');
같은 것이 있습니다 :
즉, 대신에
include('classes/*');
특정 클래스 10의 서브 클래스의 컬렉션을 포함하는 좋은 방법을 찾을 수가 없습니다 .
매우 빠른 n00b 질문입니다. PHP에서는 스크립트 디렉토리를 포함 할 수 있습니까?디렉토리의 모든 PHP 파일을 포함시키는 방법()
include('classes/Class1.php');
include('classes/Class2.php');
같은 것이 있습니다 :
즉, 대신에
include('classes/*');
특정 클래스 10의 서브 클래스의 컬렉션을 포함하는 좋은 방법을 찾을 수가 없습니다 .
foreach (glob("classes/*.php") as $filename)
{
include $filename;
}
readdir() 함수를 사용하고 파일을 루프하고 포함시키는 것이 좋습니다 (해당 페이지의 첫 번째 예 참조).
PHP 5를 사용하는 경우 autoload 대신 사용할 수 있습니다.
여기는 PHP 5의 여러 폴더에서 많은 클래스를 포함하는 방법입니다. 클래스가있는 경우에만 작동합니다.
/*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;
}
}
}
+1 자동로드로 변경합니다. [자동로드 기능] (http://php.net/manual/en/function.autoload.php), [자동로드 기능] (http://php.net/manual/en/language.oop5.autoload.php) –
이 질문은 디렉토리의 모든 것을 포함하는 것과 관련되어 있기 때문에 Autoload는 관련이 없습니다. 일반적으로 이것은 다른 디렉토리에있을 것입니다 : 예를 들어 BE 디렉토리에 정의 된 DataClass와 BL 디렉토리에 정의 된 BL.class.php. – Carmageddon
전역 사용은 해결책이 아닙니다. – Peter
이 당신은 set_include_path 사용할 수 있습니다
function include_all_php($folder){
foreach (glob("{$folder}/*.php") as $filename)
{
include $filename;
}
}
include_all_php("my_classes");
여기에는 수락 된 답변과 관련된 내용이 추가되지 않습니다. – moopet
이것이 실제로 어떤 이유로 작용 한 유일한 코드였습니다. –
카르 스텐의 코드 만 수정입니다 :
set_include_path('classes/');
디렉토리에있는 모든 PHP 파일을 자동으로 포함하지 않고'include/require '를 사용할 때'classes /'를 생략 할 수 있습니다. – Gherman
더 쓰기를하는 기능을 수행하지()를 포함하는 디렉토리에있는 파일들. 변수 범위를 잃을 수 있으며 "전역"을 사용해야 할 수도 있습니다. 파일을 반복하면됩니다.
또한 포함 된 파일의 클래스 이름이 다른 파일에 정의 된 다른 클래스로 확장 될 때 어려움을 겪을 수 있습니다. 아직 포함되지 않은 파일입니다. 그러니 조심해.
나는 아직 PHP는 뜻을 포함되지 않은 새로운 클래스를 호출 할 때마다 다음 __autoload 대신 사용 ...
function __autoload($class_name) {
require_once('classes/'.$class_name.'.class.php');
}
$user = new User();
을 클래스를 포함하지 마십시오 ...이 이전 게시물입니다하지만 실현 자동 화재 __autoload 및이를 포함위한
당신은 당신이 사용할 수있는 한 번에 각각의 클래스를 정의 할 필요없이 클래스의 무리를 포함하고자하는 경우 : 당신이 J를 할 수
$directories = array(
'system/',
'system/db/',
'system/common/'
);
foreach ($directories as $directory) {
foreach(glob($directory . "*.php") as $class) {
include_once $class;
}
}
이 방법을 클래스를 포함하는 PHP 파일에서 클래스를 정의하고 전체 목록이 아님 $thisclass = new thisclass();
모든 파일을 얼마나 잘 처리할까요? 이것으로 약간의 속도 저하가 있을지 모르겠습니다.
$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;
}
가이 파일을 포함하는 알파벳 순서를 사용하는 것을 잊지 마세요 :
당신은 디렉토리와 그 하위 디렉토리에 모두 포함합니다.
"알파벳순으로"Wrong ... "을 사용할 것임을 잊지 마십시오. 항목은 파일 시스템에 저장된 순서대로 반환됩니다."- http://php.net/manual/en/function. readdir.php – NemoStein
파일이 서로 종속되어 있고 순서가 종속성과 일치하지 않으면이 방법이 작동하지 않을 수 있습니다 –
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
});
여기 PHP 문서 추천 : Autoloading classes
NO 여기 ... 파일 사이의 종속성 include_once 문하는 재귀 함수가있는 경우 모든 하위 디렉토리에있는 모든 PHP 파일 :
$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');
include()를 사용하여보다 깔끔하게 보이는 방법. 하지만이 작업은 정상적으로 처리됩니다. 모두에게 감사드립니다. – occhiso
구성 파일을 사용하여 적절한 모듈 시스템을 구축 하겠지만, 단지 모든 것을 포함하는 것보다 훨씬 유연하다는 것을 알았 기 때문입니다. :-) – staticsan
주의는 현재 디렉토리의 파일을 포함하는 경우에만 작동합니다. get_include_path()를 통해 반복 할 수는 있지만,이 작업은 신속하게 지루합니다. – nalply