Skip to content

Latest commit

 

History

History
63 lines (39 loc) · 2.91 KB

6.0.md

File metadata and controls

63 lines (39 loc) · 2.91 KB
layout current_menu
documentation
v6

Migrating from PHP-DI 5.x to 6.0

PHP-DI 6.0 is a new major version that comes with backward compatibility breaks.

This guide will help you migrate from a 5.x version to 6.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 requires PHP 7 or greater, it is no longer compatible with PHP 5.

As a consequence if you require ocramius/proxy-manager in your project (to benefit from lazy injection), you must require v2.0 (not 1.0, which is not compatible with PHP 7).

Definitions

DI\object()

The DI\object() function helper has been removed. You should use DI\create() or DI\autowire() instead.

What should you do with your DI\object() definitions:

  • if you disabled autowiring:
    • replace it with DI\create() and everything should work
  • if you use autowiring:
    • replace it with DI\create() for definitions that replace autowiring completely (i.e. those that redefine all the parameters)
    • replace it with DI\autowire() for definitions that just define some parameters and let autowiring guess the rest of the parameters

If you have a single configuration file, that's it.

If you have multiple configuration files, for example if you have built a module system, then there is one thing to be aware of: DI\object() used to extend previous definitions. create() and autowire() do not extend previous definitions, they completely override them, and this is intended. The behavior of object() was confusing and hard to understand, the new helpers are more predictable and simple.

DI\link()

The DI\link() function helper was deprecated in 5.0. It is now completely removed. Use DI\get() instead.

Caching

PHP-DI 5 was using the Doctrine Cache library for caching definitions. Since then, PSR-6 and PSR-16 have standardized caching in PHP.

PHP-DI 6 now uses PSR-16 (the simple cache standard) instead of Doctrine. The migration should be easy:

  • you can replace completely doctrine/cache with another PHP library, like the symfony/cache component

    $cache = new Symfony\Component\Cache\Simple\ApcuCache();
    $containerBuilder->setDefinitionCache($cache);
  • or else you can use the Symfony\Component\Cache\Adapter\DoctrineAdapter class from the symfony/cache component to keep using the Doctrine cache while also making it compatible with PSR-16

    $doctrineCache = /* your current Doctrine cache */
    $cache = new Symfony\Component\Cache\Adapter\DoctrineAdapter($doctrineCache);
    $containerBuilder->setDefinitionCache($cache);

Internal changes

If you were overriding or extending some internal classes of PHP-DI, be aware that they may have changed.