PHP code example of vaibhavpandeyvpz / katora

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

    

vaibhavpandeyvpz / katora example snippets


use Katora\Container;

// Create a container
$container = new Container();

// Register a simple value
$container->set('app.name', 'My Application');

// Register a service with lazy resolution
$container->set('database', fn() => new DatabaseConnection());

// Retrieve services
$appName = $container->get('app.name');
$db = $container->get('database');

use Katora\Container;

$container = new Container();

// Simple values
$container->set('config.debug', true);
$container->set('config.timezone', 'UTC');

// Objects
$container->set('logger', new Logger());

// Lazy resolution with closures
$container->set('cache', fn() => new RedisCache());

// Direct method call
$logger = $container->get('logger');

// Check if service exists
if ($container->has('logger')) {
    $logger = $container->get('logger');
}

// Method access
$container->set('service', 'value');
$value = $container->get('service');

// Array access
$container['service'] = 'value';
$value = $container['service'];

// Property access
$container->service = 'value';
$value = $container->service;

$container->set('user.repository', function (ContainerInterface $c) {
    return new UserRepository($c->get('database'));
});

// The closure is only executed when get() is called
$userRepo = $container->get('user.repository');

$container->set('singleton.service', $container->share(function () {
    return new ExpensiveService();
}));

// First call resolves and caches
$service1 = $container->get('singleton.service');

// Subsequent calls return the cached instance
$service2 = $container->get('singleton.service');
// $service1 === $service2

$container->set('callback', $container->raw(function () {
    return 'This is a callable, not a service';
}));

// Returns the callable itself, not its result
$callback = $container->get('callback');
$result = $callback(); // Call it manually

use Katora\Container;
use Katora\ServiceProviderInterface;

class DatabaseServiceProvider implements ServiceProviderInterface
{
    public function provide(Container $container): void
    {
        $container->set('database.config', [
            'host' => 'localhost',
            'port' => 3306,
        ]);

        $container->set('database', $container->share(function (ContainerInterface $c) {
            $config = $c->get('database.config');
            return new Database($config);
        }));
    }
}

// Install the provider
$container->install(new DatabaseServiceProvider());

$container = new Container([
    'app.name' => 'My App',
    'app.version' => '1.0.0',
    'database' => fn() => new Database(),
]);

$container
    ->set('service1', 'value1')
    ->set('service2', 'value2')
    ->install(new ServiceProvider())
    ->set('service3', 'value3');

use Katora\Extra\HasContainer;
use Katora\Extra\KeepsContainer;
use Psr\Container\ContainerInterface;

class MyService implements KeepsContainer
{
    use HasContainer;

    public function doSomething(): void
    {
        $logger = $this->getContainer()->get('logger');
        $logger->info('Doing something');
    }
}

$service = new MyService();
$service->setContainer($container);