PHP code example of highperapp / container

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

    

highperapp / container example snippets


// In any PHP project
use HighPerApp\HighPer\Container\Container;

$container = new Container();
$container->bind(DatabaseInterface::class, MySQLDatabase::class);
$database = $container->get(DatabaseInterface::class);



use HighPerApp\HighPer\Container\Container;

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

// Register services
$container->bind('database', PDO::class);
$container->singleton('logger', MyLogger::class);

// Register with factory
$container->factory('redis', function() {
    return new Redis(['host' => 'localhost']);
});

// Register instance
$container->instance('config', $configObject);

// Resolve services
$database = $container->get('database');
$logger = $container->get('logger');

// Bind with concrete implementation
$container->bind(LoggerInterface::class, FileLogger::class);

// Bind as singleton
$container->singleton(DatabaseInterface::class, MySQLDatabase::class);

// Bind with factory function
$container->factory('cache', function($container) {
    $redis = $container->get('redis');
    return new CacheService($redis);
});

$container->alias('db', 'database');
$container->alias('log', LoggerInterface::class);

class UserService 
{
    public function __construct(
        private DatabaseInterface $database,
        private LoggerInterface $logger
    ) {}
}

// Register dependencies
$container->bind(DatabaseInterface::class, MySQLDatabase::class);
$container->bind(LoggerInterface::class, FileLogger::class);

// Auto-wire UserService
$userService = $container->get(UserService::class);

// Get memory usage statistics
$stats = $container->getStats();

// Object pool statistics
$poolStats = $stats['object_pool_stats'];

use HighPerApp\HighPer\Container\ObjectPool;

$container = new Container();

// Access and use object pool
$objectPool = new ObjectPool();
$objectPool->populate(ExpensiveObject::class, 10);