Download the PHP package marcosdipaolo/container without Composer

On this page you can find all versions of the php package marcosdipaolo/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package container

PSR-11 Dependency Injection Container

A production-ready, lightweight PSR-11 compliant dependency injection container for PHP with automatic constructor wiring, lifecycle management, and circular dependency detection.

Features

PSR-11 Compliant - Implements the standard ContainerInterface
Automatic Wiring - Resolves constructor dependencies without configuration
Lifecycle Management - Singleton, transient, and factory bindings
Circular Dependency Detection - Catches infinite dependency loops at resolution time
Reflection Caching - Optimized performance for repeated resolutions
Type Safety - Comprehensive error messages with actionable guidance
Fully Tested - Extensive PHPUnit test suite with high coverage
Static Analysis - PHPStan level 10 compliant

Installation

Quick Start

Usage Guide

Automatic Wiring

The container can automatically resolve classes by analyzing their constructor dependencies:

Lifecycle Management

Singleton Binding

Returns the same instance every time:

Transient Binding

Creates a new instance every time:

Factory Binding

Custom resolution logic with full control:

Interface Binding

Bind interfaces to concrete implementations:

Custom Resolution

Use factory functions for complex setup:

API Reference

Constructor

Methods

set(string $id, callable|string $concrete, string $lifecycle = 'transient'): void

Register a binding with optional lifecycle:

singleton(string $id, callable|string $concrete): void

Register a singleton binding (single instance):

transient(string $id, callable|string $concrete): void

Register a transient binding (new instance each time):

factory(string $id, callable $factory): void

Register a factory binding:

get(string $id): mixed

Resolve a service from the container:

Throws NotFoundException if the service cannot be resolved.

has(string $id): bool

Check if a service is registered:

resolve(string $id): object

Resolve a class by name without prior registration:

Throws NotFoundException if the class doesn't exist.
Throws ContainerException if the class cannot be instantiated.

clearSingletons(): void

Clear all cached singleton instances:

Useful for testing.

clearReflectionCache(): void

Clear cached reflection metadata:

Useful for debugging or dynamic code generation scenarios.

Error Handling

The container provides detailed error messages to help with debugging:

Missing Type Hints

Solution: Add a type hint or use a factory function.

Union Types

Solution: Use a factory function to explicitly choose which type to inject.

Circular Dependencies

Solution: Break the circular dependency or use lazy-loading / property injection.

Built-in Type Without Default

Solution: Provide a default value or use a factory function.

Testing

The package includes a comprehensive test suite:

Performance Considerations

Reflection Caching

The container caches reflection metadata after first resolution:

Clear the cache if working with dynamically generated classes:

Singleton Pattern

Use singletons for expensive-to-create services:

PSR-11 Compliance

This container implements the Psr\Container\ContainerInterface and follows PSR-11 standards:

Best Practices

  1. Use constructor dependency injection - Prefer constructor parameters over setter injection
  2. Type hint everything - Enable automatic wiring with proper type hints
  3. Use interfaces for bindings - Makes code more testable and maintainable
  4. Register at bootstrap - Set up bindings in a single bootstrap file
  5. Use factories for complex logic - When automatic wiring isn't enough
  6. Avoid circular dependencies - Refactor to break cycles or use lazy-loading
  7. Test with clearSingletons() - Reset state between tests

License

MIT License - see LICENSE file

Contributing

Contributions are welcome! Please ensure all tests pass and code meets PHPStan level max:


Need Help? Check the test suite for more examples and edge cases.


All versions of container with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
psr/container Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package marcosdipaolo/container contains the following files

Loading the files please wait ...