Download the PHP package xtompie/container without Composer

On this page you can find all versions of the php package xtompie/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

Container

The Container class provides essential features for managing dependencies and class instances in a PHP application. It allows you to:

Requirements

Installation

Using composer

Usage

Singleton

The Container class implements the singleton pattern, ensuring that the same instance of the container is used throughout the application. This is useful for managing global services and dependencies.

Get

The Container automatically resolves class instances using PHP's reflection capabilities. It inspects the class constructor to determine required dependencies and resolves them automatically.

Resolve

The Container class provides the resolve method, which allows you to pass specific values directly to a class's constructor when resolving dependencies. This is useful when you want to override or inject certain parameters that are not automatically resolvable by the container.

Unlike the get method, which can return shared instances, the resolve method always returns a new instance of the class, even if called multiple times. This ensures transient behavior and avoids shared state between instances.

In this example, the resolve() method manually provides the value quxx for the qux parameter, while the container automatically resolves the Foo dependency. Each call to resolve creates a new Bar instance.

Dependencies

If a class requires dependencies in its constructor, the Container resolves them as well. It inspects the constructor and creates instances of required classes.

Binding

You can bind an interface to a concrete class, allowing the container to resolve the correct implementation when the interface is requested.

Alias

The container allows you to alias one class to another, making it possible to use different class implementations interchangeably.

Shared

The container manages shared services, ensuring the same instance is returned each time the service is retrieved. This is useful for managing singletons.

Transient

Transient services return a new instance each time they are accessed. By marking a class as transient, the Container ensures that different instances are returned on every access.

You can either define a service as transient manually using the transient() method or implement the Transient interface to automatically mark a class as transient.

Alternatively, if a service class implements the Transient interface, the Container will automatically treat it as transient, meaning a new instance is returned on each retrieval:

In both cases, this ensures that transient objects are always new instances, as opposed to shared services, which return the same object every time.

Provider

Providers can be used for more complex service resolution. A provider is a class that implements the Provider interface, which allows you to control how specific services are created or managed within the container. Providers inject additional logic into the resolution process.

Here's the Provider interface:

To use a provider, first create a class that implements the Provider interface. The provide() method should return the service instance, and it will be invoked by the container when needed.

Example of a provider class:

You can then register the provider with the container and retrieve the service:

This approach allows for more complex or conditional logic when resolving services within the container.

Self-Providing Services

A service can provide its own provider by implementing the Provider interface. This allows a class to automatically supply instances for the container without needing to register the provider in the container.

If a class implements the Provider interface, the container will recognize this and automatically use it as a provider when creating instances.

In this example, the Qux class implements the Provider interface, meaning the container will automatically find the provider within the class and use it to create an instance. This removes the need to manually register the provider in the container.

Multi-binding

If you bind multiple classes, the container resolves the most concrete class available. This is useful for managing multiple levels of abstraction or class inheritance.

Call

Container class includes a call method that allows you to invoke any callback with automatic dependency injection. This method works for closures (anonymous functions), static class methods, and instance methods. The container automatically resolves and injects the required dependencies into the callback.

Using a closure (anonymous function):

In this example, the container automatically resolves the Foo dependency and injects it into the closure.

Using a static class method:

For static class methods, the container resolves the dependencies and injects them before invoking the static method.

Using an instance method:

Here, the container injects the Foo dependency into the instance method and then invokes the method on the provided object.

Call with values

The Container class includes a call method that allows you to invoke a callable with automatic dependency injection. Additionally, you can pass custom values that override the container's automatic resolution.

In this example:

callArgs

The callArgs method allows you to retrieve the arguments needed to call a given callable (function, method, etc.) with automatic dependency resolution. It inspects the callable’s parameters and resolves the necessary dependencies, optionally allowing custom values or a resolver to be used.

Custom Values

You can pass custom values to callArgs that override the container's default resolution behavior.

Custom Argument Resolver ($arg)

The callArgs method also accepts an optional $arg callable that resolves parameters before falling back to container resolution or custom values. If the $arg callable returns null, the resolution falls back to the container or the custom values.

In this case, the custom resolver takes precedence. If the resolver does not handle the argument (returns null), the container or provided custom values are used as fallback.

Key Points

Extending

You can extend the Container class to add custom functionality or specific behavior for your application.

This allows you to tailor the container to meet the specific needs of your project, adding custom logic or behavior to the dependency resolution process.


All versions of container with dependencies

PHP Build Version
Package Version
No informations.
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 xtompie/container contains the following files

Loading the files please wait ....