1. Go to this page and download the library: Download code-distortion/di-caller 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/ */
code-distortion / di-caller example snippets
namespace MyApp;
class User
{
public function __construct(
private string $firstName,
private string $lastName,
) {}
}
use CodeDistortion\DICaller\DICaller;
use MyApp\User;
$result = DICaller::new(User::class)
->registerByName('firstName', 'Bob')
->registerByName('lastName', 'Smith')
->instantiate(); // new User instance
use CodeDistortion\DICaller\DICaller;
use MyApp\User;
$user = DICaller::new(User::class)->instantiate();
// throws DICallerInstantiationException - when there are unresolved parameters
use CodeDistortion\DICaller\DICaller;
use MyApp\User;
$instantiator = DICaller::new(User::class)
->registerByName('middleName', 'John');
$user = $instantiator->canInstantiate() // false - because $firstName and $lastName are unresolved
? $instantiator->instantiate()
: null;
use CodeDistortion\DICaller\DICaller;
use MyApp\User;
$user = DICaller::new(User::class)
->registerByName('middleName', 'John')
->instantiateIfPossible();
// null - because $firstName and $lastName are unresolved
use CodeDistortion\DICaller\DICaller;
$callable = fn($param1, $param2) => "$param1 $param2";
$caller = DICaller::new($callable)
->registerByName('param3', 'something');
if ($caller->canCall()) { // false - because $param1 and $param2 are unresolved
$result = $caller->call();
}
use CodeDistortion\DICaller\DICaller;
$callable = fn($param1, $param2) => "$param1 $param2";
$result = DICaller::new($callable)
->registerByName('param3', 'something')
->callIfPossible(); // null - because $param1 and $param2 are unresolved
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.