VOOZH about

URL: https://deepwiki.com/hypervel/container/2-getting-started

⇱ Getting Started | hypervel/container | DeepWiki


Loading...
Menu

Getting Started

This document guides you through installing the hypervel/container package, setting up a container instance, and performing basic service binding and resolution operations. It covers the fundamental usage patterns needed to start using the container in your application.

For detailed information about the container's internal architecture and advanced features, see Container System. For configuration and initialization from files, see Configuration & Initialization.


Installation

The hypervel/container package is distributed via Composer and requires PHP 8.2 or higher. Install it using:


Dependencies

The package automatically installs the following dependencies:

PackageVersionPurpose
php^8.2Minimum PHP runtime version
hyperf/context~3.1.0Context management for container storage
hyperf/di~3.1.0Base dependency injection functionality

Sources: composer.json1-41


Container Initialization

The Container class extends Hyperf's base container and implements the PSR-11 ContainerInterface. You can create a container instance in two ways:

Manual Instantiation


Singleton Pattern

The container provides a global singleton instance accessible via static methods:


The singleton is stored in Hyperf\Context\ApplicationContext and is accessible throughout your application's lifecycle.

Initialization Flow Diagram


Sources: src/Container.php683-692 src/Container.php697-700


Binding Services

Binding is the process of registering how the container should resolve a service. The container supports multiple binding methods:

Basic Binding

The bind() method registers a service with the container:


Conditional Binding

Use bindIf() to register a binding only if it hasn't been registered already:


Instance Binding

Register an existing object instance as a shared service:


Binding Methods Summary

MethodDescriptionUse Case
bind($abstract, $concrete)Register a bindingStandard service registration
bindIf($abstract, $concrete)Register only if not boundDefault bindings that can be overridden
instance($abstract, $instance)Register an existing instancePre-configured objects

Binding Types Diagram


Sources: src/Container.php243-259 src/Container.php302-307 src/Container.php329-349


Resolving Services

Resolution is the process of retrieving a service instance from the container. The container provides several methods for resolution:

Using make()

The make() method creates a new instance each time it's called:


Using get()

The get() method follows PSR-11 standards and returns cached instances when available:


Using makeWith()

The makeWith() method is an alias for make():


Array Access

The container implements ArrayAccess, allowing array-style access:


Resolution Methods Comparison

MethodReturnsParametersCaching
make($abstract, $params)New instance each callSupportedNo
get($id)Cached instance if availableNot supportedYes (via parent)
makeWith($abstract, $params)New instance each callSupportedNo
$container[$abstract]Same as get()Not supportedYes (via parent)

Resolution Pipeline Diagram


Sources: src/Container.php109-132 src/Container.php139-146 src/Container.php458-460 src/Container.php702-720


Working with Aliases

Aliases allow you to reference a service by multiple names:

Creating Aliases


Checking Aliases


Alias Resolution Rules

OperationBehavior
make(alias)Automatically resolves to the original abstract
bind(alias, ...)Binds to the original abstract, not the alias
get(alias)Resolves to the original abstract
has(alias)Returns true if alias exists

Sources: src/Container.php374-383 src/Container.php231-234 src/Container.php623-628


Method Invocation with Dependency Injection

The container can invoke methods and automatically inject their dependencies using the call() method:

Basic Method Invocation


With Custom Parameters


Method Call Flow


Sources: src/Container.php437-440 src/Container.php264-267 src/Container.php272-275


Checking Service Status

The container provides several methods to check service status:

Service Status Methods


Status Check Comparison

MethodReturns true when...PSR-11
bound($abstract)Service has a binding registeredNo
resolved($abstract)Service has been instantiatedNo
has($id)Service is available (bound or alias)Yes

Sources: src/Container.php194-201 src/Container.php219-226 src/Container.php211-214


Basic Usage Example

Here's a complete example demonstrating basic container usage:


Code Entity to Container Operation Mapping


Sources: src/Container.php109-132 src/Container.php243-259 src/Container.php374-383 src/Container.php437-440


Next Steps

You now have the fundamentals needed to use the hypervel/container package. For more advanced usage:

  • Container System (#3) - Detailed documentation of the container architecture
  • Advanced Container Features (#4) - Lifecycle callbacks, extenders, and method bindings
  • Configuration & Initialization (#5) - Loading services from configuration files
  • Aspect-Oriented Programming (#6) - AOP support and aspect scanning

Sources: src/Container.php1-737 src/Contracts/Container.php1-190 composer.json1-41