1. Go to this page and download the library: Download evelikto/di library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
evelikto / di example snippets
$appContext = new \evelikto\di\AppContext(new AppConfig());
// Every class can be a configuration class, there are no base classes
class AppConfig {
// every constant in this class is considered to be a config value
// these values are injectable by name: use $MY_APP_CONFIG_VALUE as argument
const MY_APP_CONFIG_VALUE = 'config value';
const ANOTHER_CONFIG_VALUE = 1.42;
// this an alias for the interface \evelikto\mvc\Router
// pointing to the concrete class \evelikto\mvc\router\ViewRouter
// AppContext can now autowire ViewRouter if asked for a Router interface
// function xxx(Router $router) will be injected with a ViewRouter instance.
const EVELIKTO_MVC_ROUTER = '\\evelikto\\mvc\\router\\ViewRouter';
// every method is considered to be a factory method
// $pdo argument will be autowired by the container
// container will register the new dependency
// as 'databaseService' & '\app\core\DatabaseService'
public function databaseService(\PDO $pdo) {
$db = new \app\core\DatabaseService($pdo);
// do some additional initialization
return $db;
}
// Arrays can also be config values
const MVC_VIEW_ROUTER_MAPPINGS = [
'GET /' => '/index.php',
'GET /about' => '/about/index.php',
];
public function eveliktoMvcRouter($MVC_VIEW_ROUTER_MAPPINGS) {
return new ViewRouter($MVC_VIEW_ROUTER_MAPPINGS);
}
// this is another possibility to initialize interfaces
// full interface name will be converted to camel-case method name
// \evelikto\mvc\Router translates to
public function eveliktoMvcRouter() {
// inside the config file ask for config values via static::
return new ViewRouter(static::MVC_VIEW_ROUTER_MAPPINGS);
}
}
// in your index.php:
$appContext = new \evelikto\di\AppContext(new AppConfig());
// Extending base config class allows you to redefine values and factories
// for different environments.
class AppConfig {
const PDO_HOSTNAME;
const PDO_DATABASE;
const PDO_USERNAME;
const PDO_PASSWORD;
/** @return \PDO */
public function pdo() {
$hostname = static::PDO_HOSTNAME;
$database = static::PDO_DATABASE;
$username = static::PDO_USERNAME;
$password = static::PDO_PASSWORD;
return new \PDO("mysql:host=$hostname;dbname=$database", $username, $password);
}
}
class LocalConfig extends AppConfig {
const PDO_HOSTNAME = 'local_host';
const PDO_DATABASE = 'local_db';
const PDO_USERNAME = 'local_dbo';
const PDO_PASSWORD = 'local_pwd';
}
class CloudConfig extends AppConfig {
const PDO_HOSTNAME = 'cloud_host';
const PDO_DATABASE = 'cloud_db';
const PDO_USERNAME = 'cloud_dbo';
const PDO_PASSWORD = 'cloud_pwd';
}
// in your index.php:
$config = IS_CLOUD ? new CloudConfig : new LocalConfig;
$appContext = new \evelikto\di\AppContext($config);
// you may also put a number of config values into an interface
interface CloudDatabaseConsts {
const PDO_HOSTNAME = 'cloud_host';
const PDO_DATABASE = 'cloud_db';
const PDO_USERNAME = 'cloud_dbo';
const PDO_PASSWORD = 'cloud_pwd';
}
class CloudConfig extends AppConfig implements CloudDatabaseConsts {
// other cloud values and factories
}
// or just put parts of your configuration into a trait
trait MvcConfig {
public function eveliktoMvcRouter() {
return new ViewRouter([
'GET /' => '/index.php',
'GET /about' => '/about/index.php',
]);
}
}
trait DatabaseConfig {
// yes, you can autowire config values!
public function pdo($PDO_CONNECTION, $PDO_USERNAME, $PDO_PASSWORD) {
return new \PDO($PDO_CONNECTION, $PDO_USERNAME, $PDO_PASSWORD);
}
}
// combine the traits in your config class to keep it clean and maintainable.
class AppConfig {
uses MvcConfig, DatabaseConfig;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.