I have this code snippet here:
<?php
include('a.php');
include('b.php');
include('c.php');
include('d.php');
include('e.php');
?>
I would like to have a loop run through this file and execute each of these files once. How can i go about this? I am new to php.
You could do something like this...
<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
";
flush();
}
?>
</pre>
You could do something like this:
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();
Wherever your file is located (let's assumed the file that requires all of the files listed in your question is index.php
).
You're going to want to require
, or include
this file (Loader.php
) in your index.php
file. For example:
index.php
<?php
// file index.php
require_once('Loader.php');
$loader = new Loader(array("a.php", "b.php","c.php","d.php"));
$loader->init();
?>
With this setup, your index.php
file will be able to create objects, and you're code will be much more organized and modular.