1. Go to this page and download the library: Download gears/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/ */
gears / di example snippets
// lets import the class
use Gears\Di\Container;
// create a new container object
$container = new Container();
// define some services
$container['session_storage'] = function ()
{
return new SessionStorage('SESSION_ID');
};
$container['session'] = function ()
{
return new Session($this['session_storage']);
};
// get the session object
$session = $container['session'];
// define factory service
$container['session_factory'] = $container->factory(function()
{
return new Session($this['session_storage']);
});
// define container parameters / attributes
$container['cookie_name'] = 'SESSION_ID';
// protecting parameters
$container['random_func'] = $container->protect(function()
{
return rand();
});
use Gears\Di\Container;
use Gears\Di\ServiceProviderInterface;
class FooProvider implements ServiceProviderInterface
{
public function register(Container $c)
{
$c['FooService'] = function(){ return new Foo(); };
}
}
$container = new Container();
$container->register(new FooProvider());
$container['FooService']->bar();
// you can also use the container like this.
$container = new Container();
$container->session_storage = function ()
{
return new SessionStorage('SESSION_ID');
};
$container->session = function ()
{
return new Session($this->session_storage);
};
$session = $container->session;
class Mail extends Container
{
// note how we prefix the word inject.
// this tells us that the property is injectable
protected $injectTo;
// private properties however will always be
// private and can not be injected directly.
private $sendMailPath;
// so if you tried to inject fooBar it will fail
private $injectFooBar;
// from a naming standpoint I think it is best if you name the injectable
// properties such that it tells you the type that should be injected.
// however this isn't enforced.
protected $injectMessage;
protected $injectTransportService;
// this is where we can define default services for our container.
protected function setDefaults()
{
// notice how we set them without the word inject
$this->to = '[email protected]';
// I could have defined this above directly on the property
// but I would rather keep everything consistent.
$this->sendMailPath = '/bin/sendmail';
$this->message = function()
{
return new Message('Hello World');
};
// take note of the camel case property name vs the definition above.
$this->transportService = function()
{
return new SendMailTransport($this->sendMailPath);
};
// you can use factory and protect too
// note you don't have to explicitly define a class property.
// but just note that both abc and xyz are public properties.
$this->abc = $this->factory(function(){ return new Abc(); });
$this->xyz = $this->protect(function($a,$b){ return $a+$b; });
}
public function send()
{
$this->message->setTo($this->to);
return $this->transportService->send($this->message);
}
}
$mail = new Mail();
$mail->send(); // sends an email to me saying Hello World
$mail = new Mail();
$mail->to = '[email protected]';
$mail->ip = '127.0.0.1';
$mail->message = function(){ return new Message('bar'); };
$mail->transportService = function(){ return new SmtpTransport($this->ip); };
$mail->send(); // sends an email to [email protected] via 127.0.0.1 saying bar
// the above could be re written as
$mail = new Mail
([
'to' => '[email protected]',
'ip' => '127.0.0.1',
'message' => function(){ return new Message('bar'); },
'transportService' => function(){ return new SmtpTransport($this->ip); },
]);
$mail->send();
$this
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.