PHP code example of arekx / mini-di

1. Go to this page and download the library: Download arekx/mini-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/ */

    

arekx / mini-di example snippets


class TestClass1 {
   public $class2;
   
   public function __construct(TestClass2 $class2) {
        $this->class2 = $class2;
   }
}

class TestClass2 {
   public $class3;
   
   public function __construct(TestClass3 $class3) {
        $this->class3 = $class3;
   }
}

class TestClass3 {}

$class3 = new TestClass3();
$class2 = new TestClass2($class3);
$class1 = new TestClass1($class2);

class TestClass1 {
   public $class2;
}

class TestClass2 {
   public $class3;
}

class TestClass3 {}

$injector = \ArekX\MiniDI\Injector::create([
    'testObject' => 'TestClass1',
    'class2' => 'TestClass2',
    'class3' => 'TestClass3'
]);

$class1 = $injector->get('testObject');

class TestClass1 {
	public $dependsOnTestClass2;
}

class TestClass2 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => 'TestClass1',
	'dependsOnTestClass2' => 'TestClass2'
]);

$testObject = $injector->get('testObject');

echo $testObject->dependsOnTestClass2 instanceof TestClass2 ? 'Success!' : 'Fail'; // Outputs: Success!

class TestClass1 {
	public $testClass2;
	public $sharedClass3;
}

class TestClass2 {
	public $sharedClass3;
}

class TestClass3 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => 'TestClass1',
	'testClass2' => 'TestClass2',
	'sharedClass3' => ['class' => 'TestClass3', 'shared' => true],
]);

$testObject = $injector->get('testObject');

echo $testObject->sharedClass3 === $testObject->testClass2->sharedClass3 ? 'Same shared classes!' : 'Fail'; // Outputs: Same shared classes!

class TestClass1 {
	public $testClass2;
	public $mappedParam;
	public $notInjectedParameter;
}

class TestClass2 {
	public $sharedClass3;
}

class TestClass3 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => ['class' => 'TestClass1', 'dependencies' => [
	    'testClass2',
	    'mappedParam' => 'sharedClass3'
	],
	'testClass2' => 'TestClass2',
	'sharedClass3' => ['class' => 'TestClass3', 'shared' => true],
]);

$testObject = $injector->get('testObject');

echo $testObject->mappedParam === $testObject->testClass2->sharedClass3 ? 'Same shared classes!' : 'Fail'; // Outputs: Same shared classes!

class TestClass1 {
	public $testClass2;
	public $mappedParam;
	public $notInjectedParameter;
	
	public function getInjectables()
	{
	    return [
            'testClass2',
            'mappedParam' => 'sharedClass3'
        ];
	}
}

class TestClass2 {
	public $sharedClass3;
}

class TestClass3 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => ['class' => 'TestClass1', 'dependencies' => 'getInjectables',
	'testClass2' => 'TestClass2',
	'sharedClass3' => ['class' => 'TestClass3', 'shared' => true],
]);

$testObject = $injector->get('testObject');

class TestClass1 {
	public $testClass2;
	public $mappedParam;
	public $notInjectedParameter;
}

class TestClass2 {
	public $sharedClass3;
}

class TestClass3 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => ['class' => 'TestClass1', 'dependencies' => function($instance, $injector) {
	    // Do things like checking if $instance needs some additional dependencies,
	    // Or if $injector has those dependencies by calling $injector->has('key');
	    return [
            'testClass2',
            'mappedParam' => 'sharedClass3'
        ];
	},
	'testClass2' => 'TestClass2',
	'sharedClass3' => ['class' => 'TestClass3', 'shared' => true],
]);

$testObject = $injector->get('testObject');

class TestClass {
	public $class2;
	public $customParam;

	public function __construct($config = []) 
	{
		$this->customParam = $config['customParam'];
	}
}

class TestClass2 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => [
		'class' => 'TestClass',
		'config' => [
			'customParam' => "Test String"
		]
	],
	'class2' => 'TestClass2'
]);

echo $injector->get('testObject')->customParam; // Outputs: Test String

class TestClass1 {
	public $dependsOnTestClass2;
}

class TestClass2 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => function($config, $dependencies, $depInjector) {
	    // You can now do some checking with $config, $dependencies and $depInjector.
	    return new TestClass1();
	},
	'dependsOnTestClass2' => 'TestClass2'
]);

$testObject = $injector->get('testObject');

echo $testObject->dependsOnTestClass2 instanceof TestClass2 ? 'Success!' : 'Fail'; // Outputs: Success!

class TestClass1 {
	public $dependsOnTestClass2;
}

class TestClass2 {}

$injector = \ArekX\MiniDI\Injector::create([
	'testObject' => [
	    'closure' => function($config, $dependencies, $depInjector) {
            // Now $dependencies has ['dependency'] as its contents.
            return new TestClass1();
        },
        'dependencies' => ['dependency']
	],
	'dependsOnTestClass2' => 'TestClass2'
]);

$testObject = $injector->get('testObject');

echo $testObject->dependsOnTestClass2 instanceof TestClass2 ? 'Success!' : 'Fail'; // Outputs: Success!

class TestClass {
    public $paramValue;
}

$injector = \ArekX\MiniDI\Injector::create([
   'testObject' => 'TestClass',
   'paramValue' => ['value' => "Some value"]
]);

echo $injector->get('testObject')->paramValue; // Will output "Some value"

class TestClass1 
{
    public $dependency1;
    
    protected $hiddenParam;
    
    public function init() 
    {
        $this->hiddenParam = $dependency1->getValue();
    }
    
    public function getHiddenParam()
    {
        return $this->hiddenParam;
    }
}

class TestClass2 
{
   public function getValue() 
   {
        return "someValue";
   }
}

$injector = \ArekX\MiniDI\Injector::create([
   'testObject' => ['class' => 'TestClass1', 'runAfterInit' => 'init'],
   'dependency1' => 'TestClass2'
]);

$object = $injector->get('testObject');

echo $object->getHiddenParam(); // Outputs "someValue"