Download the PHP package nette/di without Composer

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

Nette Dependency Injection

Downloads this Month Tests Coverage Status Latest Stable Version License

 <!---->

Introduction

Purpose of the Dependecy Injection (DI) is to free classes from the responsibility for obtaining objects that they need for its operation (these objects are called services). To pass them these services on their instantiation instead.

Nette DI is one of the most interesting part of framework. It is compiled DI container, extremely fast and easy to configure.

Documentation can be found on the website.

 <!---->

Support Me

Do you like Nette DI? Are you looking forward to the new features?

Buy me a coffee

Thank you!

 <!---->

Installation

The recommended way to install is via Composer:

It requires PHP version 8.1 and supports PHP up to 8.4.

 <!---->

Usage

Let's have an application for sending newsletters. The code is maximally simplified and is available on the GitHub.

We have the object representing email:

An object which can send emails:

A support for logging:

And finally, a class that provides sending newsletters:

The code respects Dependency Injection, ie. each object uses only variables which we had passed into it.

Also, we have a ability to implement own Logger or Mailer, like this:

DI container is the supreme architect which can create individual objects (in the terminology DI called services) and assemble and configure them exactly according to our needs.

Container for our application might look like this:

The implementation looks like this because:

Let's instantiate Container, let it create manager and we can start spamming users with newsletters :-)

Significant to Dependency Injection is that no class depends on the container. Thus it can be easily replaced with another one. For example with the container generated by Nette DI.

 <!---->

Nette DI

Nette DI is the generator of containers. We instruct it (usually) with configuration files. This is configuration that leads to generate nearly the same class as the class Container above:

The big advantage is the shortness of configuration.

Nette DI actually generates PHP code of container. Therefore it is extremely fast. Developer can see the code, so he knows exactly what it is doing. He can even trace it.

Usage of Nette DI is very easy. Save the (above) configuration to the file config.neon and let's create a container:

and then use container to create object NewsletterManager and we can send e-mails:

The container will be generated only once and the code is stored in cache (in directory __DIR__ . '/temp'). Therefore the loading of configuration file is placed in the closure in $loader->load(), so it is called only once.

During development it is useful to activate auto-refresh mode which automatically regenerate the container when any class or configuration file is changed. Just in the constructor ContainerLoader append true as the second argument:

 <!---->

Services

Services are registered in the DI container and their dependencies are automatically passed.

All dependencies declared in the constructor of this service will be automatically passed. Constructor passing is the preferred way of dependency injection for services.

If we want to pass dependencies by the setter, we can add the setup section to the service definition:

Class of the service:

We can also add the inject: yes directive. This directive will enable automatic call of inject* methods and passing dependencies to public variables with #[Inject] attribute:

Dependency Service1 will be passed by calling the inject* method, dependency Service2 will be assigned to the $service2 variable:

However, this method is not ideal, because the variable must be declared as public and there is no way how you can ensure that the passed object will be of the given type. We also lose the ability to handle the assigned dependency in our code and we violate the principles of encapsulation.

 <!---->

Factories

We can use factories generated from an interface. The interface must declare the returning type of the method. Nette will generate a proper implementation of the interface.

The interface must have exactly one method named create. Our factory interface could be declared in the following way:

The create method will instantiate an Bar with the following definition:

The factory will be registered in the config.neon file:

Nette will check if the declared service is an interface. If yes, it will also generate the corresponding implementation of the factory. The definition can be also written in a more verbose form:

This full definition allows us to declare additional configuration of the object using the arguments and setup sections, similarly as for all other services.

In our code, we only have to obtain the factory instance and call the create method:


All versions of di with dependencies

PHP Build Version
Package Version
Requires php Version 8.1 - 8.4
ext-tokenizer Version *
ext-ctype Version *
nette/neon Version ^3.3 || ^4.0
nette/php-generator Version ^4.1.6
nette/robot-loader Version ^4.0
nette/schema Version ^1.2.5
nette/utils Version ^4.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 nette/di contains the following files

Loading the files please wait ....