1. Go to this page and download the library: Download njasm/container 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/ */
njasm / container example snippets
use Njasm\Container\Container;
$container = new Container();
namespace Very\Long\Name\Space;
interface SomeInterface {}
class SomeImplementation implements SomeInterface
{
// code here
}
$container = new Njasm\Container\Container();
$container->set('Very\Long\Name\Space\SomeInterface', new SomeImplementation());
$container->alias('Some', 'Very\Long\Name\Space\SomeInterface');
$some = $container->get('Some');
// calling methods and injecting attributes is also possible
$mailerTransportSsl = $container->get(
"Mail.Transport",
array(),
array(),
array("withSSL" => array(true))
);
$container->set(
"Complex",
function($firstName = "John", $lastName = "Doe") {
// complex logic here
// ...
$theComplexObject = new Complex($firstName, $lastName);
return $theComplexObject;
}
);
$complex = $container->get("Complex");
// injecting closure dependencies is also possible
$complexJane = $container->get("Complex", array("Jane", "Fonda"));
$container->set(
"Mail.Transport",
function() use (&$container) {
return new \Namespace\For\My\MailTransport(
$container->get("Mail.Transport.Config")
);
}
);
$container->set(
"Mail.Transport.Config",
function() {
return new \Namespace\For\My\MailTransportConfig(
"smtp.example.com",
"username",
"password",
25
);
}
);
$mailer = $container->get("Mail.Transport");
$container->singleton(
"Database.Connection",
function() {
return new \Namespace\For\My\Database(
"mysql:host=example.com;port=3306;dbname=your_db",
"username",
"password"
);
}
);
// MyDatabase is instantiated and stored, for future requests to this service,
// and then returned.
$db = $container->get("Database.Connection");
$db2 = $container->get("Database.Connection");
// $db === $db2 TRUE
$pimple; // is your instantiated pimple container
$pimple["Name"] = $pimple->factory(function() {
return "John";
}
$pimpleAdapter = new \Njasm\Container\Adapter\PimpleAdapter($pimple);
$mainContainer = new \Njasm\Container\Container();
$mainContainer->provider($pimpleAdapter);
$mainContainer->has("Name"); // TRUE
echo $mainContainer->get("Name"); // John
namespace My\Name\Space;
class Something
{
// code
}
// without registering the Something class in the container you can...
$container = new Njasm\Container\Container();
$something = $container->get('My\Name\Space\Something');
//$something instanceof 'My\Name\Space\Something' == true
//once again you can also inject dependencies when calling get method.
$something = $container->get(
"My\Name\Space\Something",
array("constructor value 1", "constructor value 2"),
array("attributeName" => "value 1"), // attributes
array("methodName" => array("value 1", "value 2"))
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.