1. Go to this page and download the library: Download abmmhasan/di-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/ */
abmmhasan / di-container example snippets
/**
* A test class where we resolving dependency for
* both constructor and defined method
*/
class TestClass
{
public function __construct(Sample1 $sample, $id)
{
return [$sample->all(),$id];
}
public function getRequest(Sample2 $sample, $pid, $sid)
{
return [$sample->all(),$pid,$sid];
}
}
/**
* Pass the class as first parameter
* In second parameter pass the value as comma separated which will be
* resolved to class __construct
*/
$class = new Container(TestClass::class,23); // TestClass: Class we resolving, 23: $id param
// or,
$class = initiate(TestClass::class,23);
/**
* Afterwards we call any methods of that class
* 'getRequest()' is a method of 'TestClass' that we are calling.
* Extra parameter can be sent as comma separated, which will be resolved to given method
*/
$value = $class->getRequest(34,43);
/**
* A test class where we resolving dependency for
* both constructor and defined method
*/
class TestClass
{
public function __construct(Sample1 $sample)
{
return [$sample->all()];
}
public function getRequest(Sample2 $sample)
{
return [$sample->all()];
}
}
/**
* Only send parameter if
$myClosure = function (Request $request, $test, $rest) {
print_r($request);
echo "I'm inside[$test,$rest]";
};
$class = new Container($myClosure, 23, 34); // Pass the closure
// or,
$class = initiate($myClosure, 23, 34);
/**
* From example 02 (same for 01 but not applicable for 03: Closure)
*/
$class = new Container(TestClass::class); // TestClass: Class we resolving
// or,
$class = initiate(TestClass::class);
/**
* Add this additional call before doing any method call
*/
$class->_noInject();
/**
* Now do calls as usual
*/
$value = $class->getRequest();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.