Organize classes in folders and auto/lazy load them as attributes.
Install via Composer
$ composer require web6/class-collection
Configure autoloading by including Composer's generated file :
include_once('vendor/autoload.php');
Create classes and save them in a folder.
class App {
use \W6\ClassCollection\ClassCollectionTrait;
public $message = 'Not inited';
protected function init() {
$this->message = 'Inited';
}
}
Anywhere in your application you can request the same instance of the class.
$app = App::instance();
echo $app->message;