私はここにこのコードスニペットを持っています:
<?php
include('a.php');
include('b.php');
include('c.php');
include('d.php');
include('e.php');
?>
このファイルをループして、これらの各ファイルを1回実行したいと思います。どうすればこれについて行くことができますか?私はphpを初めて使用します。
私はここにこのコードスニペットを持っています:
<?php
include('a.php');
include('b.php');
include('c.php');
include('d.php');
include('e.php');
?>
このファイルをループして、これらの各ファイルを1回実行したいと思います。どうすればこれについて行くことができますか?私はphpを初めて使用します。
あなたはこのようなことをすることができます...
<pre>
<?php
set_time_limit (0);
$files = array('a', 'b', 'c', 'd', 'e',);
foreach($files as $f){
include_once $f.".php";
echo "finished file $f\n";
flush();
}
?>
</pre>
あなたはこのようなことをすることができます:
class Loader
{
protected $files = array();
public function __construct($files)
{
$this->files = $files;
}
public function Init()
{
foreach($this->files as $file)
{
if(file_exists($file))
{
require_once($file);
}
}
}
$loader = new Loader(array("a.php", "b.php","c.php","d.php"));
$loader->init();
ファイルがどこにあっても(質問にリストされているすべてのファイルを必要とするファイルがであると仮定しましょうindex.php
)。
require
、またはinclude
このファイル(Loader.php
)をファイルに入れたいと思うでしょうindex.php
。例えば:
index.php
<?php
// file index.php
require_once('Loader.php');
$loader = new Loader(array("a.php", "b.php","c.php","d.php"));
$loader->init();
?>
この設定により、index.php
ファイルでオブジェクトを作成できるようになり、コードがはるかに整理されてモジュール化されます。