PHPackages                             dastur/capsule - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dastur/capsule

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dastur/capsule
==============

A dependency injection container.

v1.0.0(9y ago)014MITPHPPHP &gt;=5.6

Since Jun 16Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Dastur1970/Capsule)[ Packagist](https://packagist.org/packages/dastur/capsule)[ RSS](/packages/dastur-capsule/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Capsule
=======

[](#capsule)

Capsule is a PHP dependency injection container. It allows you to bind instances to the container so you can retrieve them when they are required. If you would like to make sure none of your dependencies are hidden, you can use the automatic dependency injection provided by this package.

Table of Contents
-----------------

[](#table-of-contents)

- [Install](#install)
- [Usage](#usage)
    - [Binding](#binding)
    - [Resolving](#resolving)
    - [Binding with Making](#binding-with-making-resolving)
    - [Destroying](#destroying)
- [Contribute](#contribute)
- [License](#license)

Install
-------

[](#install)

### Composer

[](#composer)

Run the following command from your projects base directory:

```
composer require dastur\dastur

```

Usage
-----

[](#usage)

To start using capsule, create a new Capsule instance.

```
$capsule = new Capsule();
```

### Binding

[](#binding)

To bind a class to the container, use the bind method

```
$capsule->bind(string $name, mixed $path, callable $value, boolean $singleton = false)
```

$name parameter is the bindings alias. $path parameter is a class constant or string (that contains the full namespace). This is used by the Capsule when it needs to resolve a classes dependencies. $value parameter is a callable that returns the service that you are binding. $singleton parameter is whether or not you want the instance to only be resolved once.

Example:

```
$capsule->bind('service', Service::class, function() {
  return new Service();
});
```

By default, Capsule creates a new instance every time you get something from the container. If you want something to only be resolved once, use the `singleton()` method.

```
$capsule->singleton('service', Service::class, function() {
  return new Service();
});
```

### Resolving

[](#resolving)

When you need to get an instance from the container, use the get method

```
$capsule->get(mixed $name)
```

$name parameter is the name or class constant for the class that is being resolved. This method will only resolve classes that are stored in the Capsule.

Example:

```
$capsule->get('service') or $capsule->get(Service::class)
```

Alternatively, you may use the following syntax:

```
$capsule->service or $capsule['service']
```

If you need to create a class that is not binded in the container, use the make method.

```
$capsule->make(mixed $path, array $parameters = [])
```

$path is the class constant for the class you are resolving. You may use a string for this parameter as well, but make sure it is formatted correctly or else an error will be thrown. $parameters parameter is an array of overrides you would like to define.

For example:

Lets say you have the class `Service` that is dependant upon `AnotherService` and also requires a random string in its constructor.

```
