1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
---
layout: documentation
current_menu: v7
---
# Migrating from PHP-DI 6.x to 7.0
PHP-DI 7.0 is a new major version that comes with backward compatibility breaks.
This guide will help you migrate from a 6.x version to 7.0. It will only explain backward compatibility breaks, it will not present the new features (read the release notes or the blog post for that).
## PHP version
PHP-DI now requires PHP 8.0 or greater. If you are using an older version, you can of course still use PHP-DI 6.
## Container creation
The container can now be created with sane defaults without using the `ContainerBuilder` class (though it's not mandatory):
```php
$container = new \DI\Container();
// With definitions:
$container = new \DI\Container([
\Psr\Log\LoggerInterface::class => get(MyLogger::class),
]);
```
Related to that: `\DI\ContainerBuilder::buildDevContainer()` method is now obsolete and has been removed. Replace it with:
```diff
- $container = \DI\ContainerBuilder::buildDevContainer();
+ $container = new \DI\Container();
```
## Annotations (`@Inject`) have been replaced by PHP attributes (`#[Inject]`)
Now that PHP 8.0 and up [supports attributes natively](https://www.php.net/manual/fr/language.attributes.overview.php), these are read instead of phpdoc annotations.
Additionally, now that PHP supports typed properties, PHP-DI will stop reading types from phpdoc.
Here is an example on how to migrate from PHP-DI 6 to PHP-DI 7:
- **before:**
```php
// Container configuration
$containerBuilder = new \DI\ContainerBuilder;
$containerBuilder->useAnnotations(true);
```
```php
class Example
{
/**
* @Inject
* @var Foo
*/
private $property;
/**
* @Inject
* @param Foo $param
*/
public function method($param)
{
}
}
```
- **after:**
```php
// Container configuration
$containerBuilder = new \DI\ContainerBuilder;
$containerBuilder->useAttributes(true);
```
```php
use DI\Attribute\Inject;
class Example
{
#[Inject]
private Foo $property;
#[Inject]
public function method(Foo $param)
{
}
}
```
*Note: remember to import the attribute class via `use DI\Attribute\Inject;`.*
Read more about attributes in the PHP-DI documentation: [PHP-DI attributes](../attributes.md).
## Internal changes
If you were overriding or extending some internal classes of PHP-DI, be aware that they may have changed.
|