1. Go to this page and download the library: Download redcatphp/strategy 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/ */
redcatphp / strategy example snippets
$a = new A(new B, new C, new D(new E, new F));
$a = $di->get('A');
class A {
private $a;
private $b;
private $c;
function \_\_construct(A $a, B $b, C $c, D $d){
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
}
}
class D {
private $e;
private $f;
function \_\_construct(E $e, F $f){
$this->e = $e;
$this->f = $f;
}
}
$a = new A(new B, new C, new D(new E, new F));
$a = $di->get('A');
class C {
private $x;
function \_\_construct(X $x){
$this->x = $x;
}
}
class A {
private $b;
private $c;
private $d;
function \_\_construct(){
$this->b = new B;
$this->c = new C;
$this->d = new D(new E, new F);
}
}
class A {
private $b;
private $c;
private $d;
function \_\_construct(B $b, C $c, D $d){
$this->b = $b;
$this->c = $c;
$this->d = $d;
}
}
class B {
function \_\_construct(PDO $pdo, C $c){
}
}
class ExistingA {
function \_\_construct(B $b){
}
}
$di = \\Strategy\\Di::getInstance(); //global shared instance
$di = new \\Strategy\\Di; //classical new instance
$di->get('My\\Class');
\\Strategy\\Di::getInstance()->get('My\\Class'); //global shared instance used via object
\\Strategy\\Di::make('My\\Class'); //global shared instance used via static call
class A {
private $b;
function \_\_construct(B $b) {
$this->b = $b;
}
}
class B {
private $c,$d;
function \_\_construct(C $c, D $d) {
$this->c = $c;
$this->d = $d;
}
}
class C {
}
class D {
private $e;
function \_\_construct(E $e) {
$this->e = $e;
}
}
class E {
}
$a = $di->get('A');
print\_r($a);
A Object
(
[b:A:private] => B Object
(
[c:B:private] => C Object
(
)
[d:B:private] => D Object
(
[e:D:private] => E Object
(
)
)
)
)
class A {
public $name;
public $b;
function \_\_construct(B $b, $name) {
$this->name = $name;
$this->b = $b;
}
}
//create a rule to apply to shared object
$rule = ['shared' => true];
//Apply the rule to instances of PDO
$di->addRule('PDO', $rule);
//Now any time PDO is requested from Strategy, the same instance will be returned
$pdo = $di->get('PDO');
$pdo2 = $di->get('PDO');
var\_dump($pdo === $pdo2); //TRUE
//And any class which asks for an instance of PDO will be given the same instance:
class MyClass {
public $pdo;
function \_\_construct(PDO $pdo) {
$this->pdo = $pdo;
}
}
$myobj = $di->get('MyClass');
var\_dump($pdo === $myobj->pdo); //TRUE
$rule = [
//Mark the class as shared so the same instance is returned each time
'shared' => true,
//The constructor arguments that will be supplied when the instance is created
'construct' => ['mysql:host=127.0.0.1;dbname=mydb', 'username', 'password']
];
//Apply the rule to the PDO class
$di->addRule('PDO', $rule);
//Now any time PDO is requested from Strategy, the same instance will be returned
//And will havebeen constructed with the arugments supplied in 'construct'
$pdo = $di->get('PDO');
$pdo2 = $di->get('PDO');
var\_dump($pdo === $pdo2); //TRUE
//And any class which asks for an instance of PDO will be given the same instance:
class MyClass {
public $pdo;
function \_\_construct(PDO $pdo) {
$this->pdo = $pdo;
}
}
class MyOtherClass {
public $pdo;
function \_\_construct(PDO $pdo) {
$this->pdo = $pdo;
}
}
//Note, Strategy is never told about the 'MyClass' or 'MyOtherClass' classes, it can
//just automatically create them and inject the
class A {
function \_\_construct(Iterator $iterator) {
}
}
class B implements Iterator {
//...
}
//When a constructor asks for an instance of Iterator pass it an instance of B instead
$rule = ['substitutions' => ['Iterator' => new Expander('B')]];
$di->addRule('A', $rule);
$a = $di->get('A');
$rule = ['substitutions' =>
['Iterator' => new Expander(function() {
return new DirectoryIterator('/tmp');
})]
]
];
$di->addRule('A', $rule);
$a = $di->get('A');
$namedDirectoryIteratorRule = [
//An instance of the DirectoryIterator class will be created
'instanceOf' => 'DirectoryIterator',
//When the DirectoryIterator is created, it will be passed the string '/tmp' as the constructor argument
'construct' => ['/tmp']
];
//Create a rule under the name "$MyDirectoryIterator" which can be referenced as a substitution for any other rule
$di->addRule('$MyDirectoryIterator', $namedDirectoryIteratorRule);
//This tells the DI Container to use the configuration for $MyDirectoryIterator when an Iterator is asked for in the constructor argument
$aRule = ['substitutions' =>
[
'Iterator' => new Expander('$MyDirectoryIterator')
]
];
//Apply the rule to the A class
$di->addRule('A', $aRule);
//Now, when $a is created, it will be passed the Iterator configured as $MyDirectoryIterator
$a = $di->get('A');
class A {
}
class B extends A {
}
//Mark instances of A as shared
$aRule = ['shared' => true];
$di->addRule('A', $aRule);
//Get the rule currently applied to 'B' objects
$bRule = $di->getRule('B');
//And B instances will also be shared
var\_dump($bRule['shared']); //TRUE
//And to test it:
$b1 = $di->get('B');
$b2 = $di->get('B');
var\_dump($b1 === $b2); //TRUE (they are the same instance)
class A {
}
class B extends A {
}
//This time mark A as shared, but turn off rule inheritance
$aRule = ['shared' => true, 'inherit' => false];
$di->addRule('A', $rule);
$bRule = $di->getRule('B');
//Now, B won't be marked as shared as the rule applied to A is not inherited
var\_dump($bRule['shared']); //FALSE
//And to test it:
$b1 = $di->get('B');
$b2 = $di->get('B');
var\_dump($b1 === $b2); //FALSE (they are not the same instance)
class A {
function \_\_construct(B $b, $foo, $bar) {
}
}
class Foo {
public $pdo;
function \_\_construct(PDO $pdo) {
$this->pdo = $pdo;
}
}
//When PDO is type hinted, supply an instance of MyPDO instead
$rule = ['substitutions' => ['PDO' => new Expander('MyPDO')]];
//Apply the rule to every class
$di->addRule('\*', $rule);
$foo = $di->get('Foo');
echo get\_class($foo->pdo); // "MyPDO"
class DataCopier {
function \_\_construct(PDO $database1, PDO $database2) {
}
}
//A rule for the default PDO object
$rule = [
'shared' => true,
'construct' = ['mysql:host=127.0.0.1;dbname=mydb', 'username', 'password']
];
$di->addRule('PDO', $rule);
//And a rule for the second database
$secondDBRule = [
'shared' => true,
'construct' = ['mysql:host=externaldatabase.com;dbname=foo', 'theusername', 'thepassword'],
//This rule will create an instance of the PDO class
'instanceOf' => 'PDO'
];
//Add named instance called $Database2
//Notice that the name being applied to is not the name of class
//but a chosen named instance
$di->addRule('$Database2', $secondDBRule);
//Now set DataCopier to use the two different databases:
$dataCopierRule = [
'construct' => [ //Set the constructor parameters to the two database instances.
new Expander('PDO'),
new Expander('$Database2')
]
];
$di->addRule('DataCopier', $dataCopierRule);
$dataCopier = $di->get('DataCopier');
class A {
public $b, $c;
function \_\_construct(B $b, C $c) {
}
}
class B {
public $d;
function \_\_construct(D $d) {
$this->d = $d;
}
}
class C {
public $d;
function \_\_construct(D $d) {
$this->d = $d;
}
}
class D {}
$rule = [
'shareInstances' = ['D']
];
$di->addRule('A', $rule);
//Create an A object
$a = $di->get('A');
//Anywhere that asks for an instance D within the tree that existis within A will be given the same instance:
//Both the B and C objects within the tree will share an instance of D
var\_dumb($a->b->d === $a->c->d); //TRUE
//However, create another instance of A and everything in this tree will get its own instance of D:
$a2 = $di->get('A');
var\_dumb($a2->b->d === $a2->c->d); //TRUE
var\_dumb($a->b->d === $a2->b->d); //FALSE
var\_dumb($a->c->d === $a2->c->d); //FALSE
$container['session\_storage'] = function ($c) {
return new SessionStorage('SESSION\_ID');
};
$container['session'] = function ($c) {
return new Session($c['session\_storage']);
};
$session = $container['session']; //get the session object
$session2 = $container['session']; //get the same session object
var\_dump($session===$session2); //will show true
$container['session'] = $container->factory(function ($c) {
return new Session($c['session\_storage']);
});
$session = $container['session']; //get a session object
$session2 = $container['session']; //get a new session object
var\_dump($session===$session2); //will show false