I'm currently developing an application with the FlightPHP framework and wondering how am I able to inject FlightPHP into my custom class so that I am able to use specific classes I have injected into it's dependency container.
use Flight;
use Logger;
class DB{
public function __construct(...){
$this->app = $app; // Flight:: instance
}
public function doStuff($stuff){
return $this->app->log()->function($stuff);
}
}
Flight::register('log', 'Logger', ['app'], function($log) {
return $log->pushHandler(new StreamHandler('app.log'));
});
Flight::register('database', 'DB', array($data), function($db) {
return $db;
});
I'm attempting to inject Flight into my database class constructor so that I am able to use the log function which was previously injected into the Flight dependency container.
The "Logger" works in the index.php when used under the Flight instance "Flight::log()->function("test");
", however when I attempt to use it in another scope(within the Database class), it doesn't allow me to use it in the context of "Flight".
Update:
Flight::register('log', 'Monolog\Logger', ['app'], function($log) {
return $log->pushHandler(new StreamHandler('app.log'));
});
class DB{
function __construct(Monolog\Logger $engine){
#var_dump($engine);
$engine->addInfo("injected"); // works
}
}
Flight::register('database', 'DB', array(Flight::log()), function($db) {
return $db;
});
Flight::database();
Is correct usage?