1. Go to this page and download the library: Download mykemeynell/reflection 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/ */
mykemeynell / reflection example snippets
use mykemeynell\Reflection\Application\Container;
$container = new Container();
class Database {}
class UserRepository {
public function __construct(public Database $db) {}
}
$userRepository = $container->make(UserRepository::class);
class Logger {}
class OrderService {
public function __construct(public Logger $logger) {}
}
$service = $container->make(OrderService::class);
class HttpClient {
public function __construct(public int $timeout = 30) {}
}
$client = $container->make(HttpClient::class); // $client->timeout === 30
$container->when(ReportGenerator::class)
->needs(StorageInterface::class)
->give(fn (Container $app) => new S3Storage('bucket-name'));
class ApiService {
public function __construct(
public HttpClient $client,
public string $apiKey,
public int $timeout = 30
) {}
}
$service = $container->make(
ApiService::class,
apiKey: 'my-token',
timeout: 60,
);
use mykemeynell\Reflection\Attributes\Inject;
final readonly class ReportService
{
public function __construct(
#[Inject(S3Storage::class)]
public StorageInterface $storage,
) {}
}
use mykemeynell\Reflection\Attributes\Inject;
use mykemeynell\Reflection\Attributes\Override;
final readonly class ReportService
{
public function __construct(
#[Inject(S3Storage::class), Override]
public StorageInterface $storage,
) {}
}
use mykemeynell\Reflection\Attributes\Value;
final readonly class HttpClient
{
public function __construct(
#[Value(3)]
public int $retries,
#[Value(30)]
public int $timeout,
) {}
}
use mykemeynell\Reflection\Attributes\Singleton;
#[Singleton]
final class DatabaseConnection {}
class ServiceA {
public function __construct(public ServiceB $b) {}
}
class ServiceB {
public function __construct(public ServiceA $a) {}
}
$container->make(ServiceA::class);
// Throws ContainerException: Circular dependency detected while resolving [ServiceA]: ServiceA -> ServiceB -> ServiceA.
use Psr\Container\ContainerInterface;
function bootstrap(ContainerInterface $container): void {
if ($container->has(Router::class)) {
$router = $container->get(Router::class);
}
}
use function mykemeynell\Reflection\Helpers\app;
// Retrieve container instance
$container = app();
// Resolve service
$mailer = app(MailerInterface::class);
// Resolve with parameters
$service = app(ApiService::class, apiKey: 'token', timeout: 45);
// Resolve closure
$result = app(fn (Container $app) => $app->make(Logger::class));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.