All the questions here refer to how import files into a directory, I'm looking for a smart way that allows me to import all the classes in a single class. Specifically, suppose that I have a structure like this:
\ Root 'Main folder
Bootstrap.php 'This is the main class
\System
Core.php
Language.php
Helper.php
Now in Bootstrap.php
for import the Core, Language, Helper
classes I should do something like this:
include "System/Core.php";
include "System/Languages.php";
include "System/Helper.php;"
private $_core;
private $_languages;
private $_helper;
public function __construct()
{
$this->_core = new Core();
$this->_languages = new Languages();
$this->_helper = new Helper();
}
Suppose that the files are more than 20, it would be a pain to import everything. So what would be a smart way to import all the classes and access their functions?
I am not really sure why you would like to do that but it can easily be done:
foreach(glob('System/*.php') as $file) include_once "System/$file";
May be you should have a look at autoloading: http://php.net/manual/en/language.oop5.autoload.php
// Register autoloader
spl_autoload_register(function ($class_name) {
$fullPath = 'System/' . $class_name . '.php';
if(file_exists($fullPath)) include $fullPath;
});
// Simply create a new object, class will be included by autoloader
$helper = New Helper();
This is a really simple autoloader but I hope you get the idea of it.