. . factory . , $obj = new MyClass(); , , factory MyClass, .
, . , . MyClass , . .
Pimple Aura DI, .
Here is an example for Pimple (if your project uses a composer). Go to the root of the project and get a pimple:
$ composer require pimple/pimple ~3.0
Create and configure the container in the early stages of your application:
use Pimple\Container;
$dic = new Container();
$dic['config'] = function ($c) {
return ['base_url' => '/mypath/'];
};
$dic['myclass'] = function ($c) {
return new MyClass($c['config']);
};
Now you can get your class anywhere in the application by simply typing:
$obj = $dic['myclass'];
Your MyClass signature should look like this:
private $config;
public function __construct(array $config)
{
$this->config = $config;
}
public function myfunc() {
print_r($this->config);
}
edigu source
share