回答編集履歴
1
追記
answer
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
setting.phpがindex.phpやlogin.phpと同じ階層にあると仮定します。
|
2
|
-
setting.php内に以下のコードを記述
|
2
|
+
setting.php内に以下のコードを記述して下さい。
|
3
|
+
そして、各index.phpやlogin.php内でsetting.phpを読み込んで下さい。
|
3
4
|
|
4
5
|
$scan_dirsに自動で読み込ませたいフォルダを配列で渡しています。
|
5
6
|
以下コードはPHP5.4以上で動作します。
|
7
|
+
```PHP
|
8
|
+
spl_autoload_register(function ($class_name) {
|
9
|
+
$scan_dirs = [
|
10
|
+
__dir__.'/class/check',
|
11
|
+
__dir__.'/class/get',
|
12
|
+
];
|
13
|
+
foreach ($scan_dirs as $scan_dir) {
|
14
|
+
$include_path = $scan_dir.'/'.$class_name.'.php';
|
15
|
+
if (is_readable($include_path)) {
|
16
|
+
require_once $include_path;
|
17
|
+
break;
|
18
|
+
}
|
19
|
+
}
|
20
|
+
});
|
21
|
+
```
|
6
22
|
|
7
|
-
|
23
|
+
新たに読み込むディレクトリを増やしたい場合は、以下のように配列に追加すれば良いです。
|
8
24
|
```PHP
|
9
25
|
spl_autoload_register(function ($class_name) {
|
10
26
|
$scan_dirs = [
|
11
27
|
__dir__.'/class/check',
|
12
28
|
__dir__.'/class/get',
|
29
|
+
// ↓新しく3つの読み込むディレクトリを増やす
|
30
|
+
__dir__.'/class/newdir1',
|
31
|
+
__dir__.'/class/newdir2',
|
32
|
+
__dir__.'/class/newdir3',
|
13
33
|
];
|
14
34
|
foreach ($scan_dirs as $scan_dir) {
|
15
35
|
$include_path = $scan_dir.'/'.$class_name.'.php';
|
@@ -19,4 +39,28 @@
|
|
19
39
|
}
|
20
40
|
}
|
21
41
|
});
|
22
|
-
```
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
【イメージ】
|
46
|
+
// インスタンス化
|
47
|
+
$user = new User();
|
48
|
+
↑Userクラスがまだ読み込まれていない場合は、通常エラーが発生しますが、オートロード設定をしていると、以下の様な流れになります。
|
49
|
+
|
50
|
+
```PHP
|
51
|
+
// ↓Userが引数として渡される
|
52
|
+
spl_autoload_register(function ('User') {
|
53
|
+
```
|
54
|
+
```PHP
|
55
|
+
// ↓以下のようなイメージ
|
56
|
+
foreach ($scan_dirs as $scan_dir) {
|
57
|
+
$include_path = $scan_dir.'/User.php';
|
58
|
+
if (is_readable($include_path)) {
|
59
|
+
require_once $include_path;
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
わかりにくい説明ですが、このような流れです。
|