1. Go to this page and download the library: Download byteever/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/ */
byteever / container example snippets
use ByteEver\Container\Container;
// Create a container instance
$container = Container::create();
// Bind a service
$container->bind('logger', MyLogger::class);
// Resolve the service
$logger = $container->make('logger');
// Register an instance
$container->instance('database', new Database($config));
// Get the instance
$db = $container->get('database');
// In your main plugin file
use ByteEver\Container\Container;
class MyPlugin {
private Container $container;
public function __construct() {
// REQUIRED: Pass the main plugin file (__FILE__)
$this->container = Container::create(__FILE__);
$this->registerServices();
}
private function registerServices(): void {
// Register your plugin services
$this->container->singleton('admin', Admin::class);
$this->container->singleton('frontend', Frontend::class);
$this->container->bind('api', API::class);
}
public function getContainer(): Container {
return $this->container;
}
}
// Bind a concrete class
$container->bind('logger', FileLogger::class);
// Bind with a closure
$container->bind('mailer', function($container) {
return new Mailer($container->get_config('smtp'));
});
// Bind interface to implementation
$container->bind(LoggerInterface::class, FileLogger::class);
// Register as singleton
$container->singleton('database', Database::class);
// Both calls return the same instance
$db1 = $container->make('database');
$db2 = $container->make('database');
// $db1 === $db2 (true)
// Register existing instance
$logger = new Logger('/path/to/log');
$container->instance('logger', $logger);
// Auto-instantiation (new feature!)
$container->instance(Database::class); // Automatically creates and registers instance
// Bulk registration
$container->instance([
'cache' => new RedisCache(),
'session' => new SessionManager()
]);
class UserService {
public function __construct(
private Database $database,
private Logger $logger,
private CacheInterface $cache
) {}
}
class Database {
public function __construct(private string $host = 'localhost') {}
}
// Register dependencies
$container->bind(CacheInterface::class, RedisCache::class);
$container->instance('logger', new Logger());
// Auto-wiring in action - resolves all dependencies automatically
$userService = $container->make(UserService::class);
// Tag services
$container->tag('handlers', [
'user_handler',
'post_handler',
'comment_handler'
]);
// Or tag individually
$container->bind('user_handler', UserHandler::class);
$container->tag('handlers', 'user_handler');
// Get all services with a tag
$handlers = $container->tagged('handlers');
foreach ($handlers as $handler) {
$handler->process();
}
$container->bind(DatabaseInterface::class, MySQLDatabase::class);
$container->alias(DatabaseInterface::class, 'db');
// Now you can use either
$database = $container->make(DatabaseInterface::class);
$database = $container->make('db'); // Same instance
// The container can extract plugin metadata
$container = Container::create(__FILE__); // Pass your main plugin file
// Access plugin information
$pluginName = $container->get_config('name');
$version = $container->get_config('version');
$textDomain = $container->get_config('text_domain');
class MyPlugin {
public function __construct() {
$this->container = Container::create(__FILE__);
$this->registerHooks();
}
private function registerHooks(): void {
add_action('init', [$this, 'init']);
add_action('admin_init', [$this, 'adminInit']);
}
public function init(): void {
$frontend = $this->container->make('frontend');
$frontend->init();
}
public function adminInit(): void {
$admin = $this->container->make('admin');
$admin->init();
}
}
class DatabaseServiceProvider {
public function register(Container $container): void {
$container->singleton('database', function($container) {
return new Database(
$container->get_config('database.host'),
$container->get_config('database.port'),
$container->get_config('database.credentials')
);
});
$container->alias('database', 'db');
}
}
// Register the service provider
$provider = new DatabaseServiceProvider();
$provider->register($container);
// Bind different implementations based on environment
if ($container->get_config('app.debug')) {
$container->bind(LoggerInterface::class, DebugLogger::class);
} else {
$container->bind(LoggerInterface::class, ProductionLogger::class);
}
// You can pass additional parameters when resolving
$userService = $container->make(UserService::class, [
'connection' => 'mysql-read-replica'
]);
// Good
$container->bind(LoggerInterface::class, FileLogger::class);
// Instead of
$container->bind('logger', FileLogger::class);
class MyPlugin {
public function __construct() {
$this->container = Container::create(__FILE__);
$this->registerServices(); // Do this early
$this->registerHooks();
}
}
class PluginServiceProvider {
public function register(Container $container): void {
// Group related service registrations
$this->registerDatabase($container);
$this->registerCache($container);
$this->registerLogging($container);
}
}
// Let the container handle dependencies
class AdminController {
public function __construct(
private UserRepository $users,
private Logger $logger,
private Validator $validator
) {}
}
// Just resolve - dependencies are auto-injected
$controller = $container->make(AdminController::class);
class UserServiceTest extends TestCase {
public function testUserCreation(): void {
$container = Container::create();
// Mock dependencies
$mockDatabase = $this->createMock(Database::class);
$mockLogger = $this->createMock(Logger::class);
$container->instance('database', $mockDatabase);
$container->instance('logger', $mockLogger);
$userService = $container->make(UserService::class);
// Test your service with mocked dependencies
$this->assertInstanceOf(UserService::class, $userService);
}
}
// Main plugin class
class MyAwesomePlugin {
private Container $container;
public function __construct() {
$this->container = Container::create(__FILE__);
$this->bootstrap();
}
private function bootstrap(): void {
// Register core services
$this->registerCore();
// Register WordPress hooks
$this->registerHooks();
// Initialize modules
$this->initializeModules();
}
private function registerCore(): void {
$this->container->singleton('plugin', $this);
$this->container->singleton('loader', HookLoader::class);
$this->container->singleton('admin', AdminModule::class);
$this->container->singleton('frontend', FrontendModule::class);
$this->container->singleton('api', APIModule::class);
}
private function registerHooks(): void {
$loader = $this->container->make('loader');
$loader->run();
}
private function initializeModules(): void {
if (is_admin()) {
$this->container->make('admin')->init();
} else {
$this->container->make('frontend')->init();
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.