- #791 Support PHP 8.1, remove support for PHP 7.2
- Fix potential regression introduced when fixing #582
- Fix #526: Support optional parameters in factories
- #585 Add support for PHP-Parser 4.0
- #582 Register
ContainerInterface
to point to the wrapper container if it was defined
This is the complete change log. You can also read the migration guide for upgrading or the blog article to see what's new.
Improvements:
- #494 The container can now be compiled for optimum performances in production
- #294, #349, #449:
DI\object()
has been replaced by more specific and less ambiguous helpers:DI\create()
creates an object, overrides autowiring and previous definitionsDI\autowire()
autowires an object and allows to override specific constructor and method parameters
- The container can now be built without parameters:
new Container()
- Definitions can be nested:
- #487 Closures in the config are now always interpreted as factories, even when nested inside other definitions
- #242 Error in case a definition is not indexed by a string
- #505 Debug container entries
- #564 Caching was made almost entirely obsolete by the container compilation, however there is still a caching system entirely rebuilt over APCu for covering the last cases that compilation could not address (see php-di.org/doc/performances.html)
Fixes:
- #499 & #488 Standardize resolution of nested definitions everywhere. In PHP-DI 5, definitions could be nested in some places (e.g. use a get() in an object definition, etc.). However it did not behave everywhere the same, for example it didn't work for sub-definitions in arrays. Now in PHP-DI 6 all nested definitions will all be recognized and resolved correctly everywhere. Since #494 (compiled container) performance will not be affected so we can implement a more robust behavior.
- #343 Autowiring and Annotations do not work for
object()
inside arrays: it now works with the newcreate()
andautowire()
helpers
BC breaks:
- PHP 7 or greater is required and HHVM is no longer supported
DI\object()
has been removed, useDI\create()
orDI\autowire()
instead- #409: Scopes are removed, read more in the scopes documentation.
- Caching was replaced by compiling the container:
ContainerBuilder::setDefinitionCache()
was removed, useContainerBuilder::enableCompilation()
instead. - #463 & #485: Container-interop support was removed, PSR-11 is used instead (by @juliangut)
- The deprecated
DI\link()
helper was removed, usedDI\get()
instead - #484 The deprecated
\DI\Debug
class has been removed. Definitions can be cast to string directly - The exception
DI\Definition\Exception\DefinitionException
was renamed toDI\Definition\Exception\InvalidDefinition
- The exception
DI\Definition\Exception\AnnotationException
was renamed toDI\Definition\Exception\InvalidAnnotation
- #516
DI\InvokerInterface
was removed in favor ofInvoker\InvokerInterface
.
Be also aware that internal classes or interfaces may have changed.
- Fix #554:
Container::make()
fails when combined withdecorate()
.
Fixup of 5.4.4.
- #531: performance improvement.
This release was broken because it was tagged against the wrong branch.
- #531: performance improvement.
- #467: register the container against the PSR ContainerInterface
- Minor patch to add the
provide: psr/container-implementation
tocomposer.json
.
- PSR-11 compliance
Note that PHP-DI was already compliant with PSR-11 because it was implementing container-interop, and container-interop 1.2 extends PSR-11. This new version just makes it more explicit and will allow to drop container-interop support in the next major versions.
Read the news entry.
New features:
- #362 implemented in #428, #430, #431 and #432: factory parameters can now be configured, for example:
Read the factories documentation to learn more. Feature implemented by @predakanga.
return [ 'Database' => DI\factory(function ($host) {...}) ->parameter('host', DI\get('db.host')), ];
Improvements:
- #429: performance improvements in definition resolution (by @mnapoli)
- #421: once a
ContainerBuilder
has built a container, it is locked to prevent confusion when adding new definitions to it (by @mnapoli) - #423: improved exception messages (by @mnapoli)
Read the news entry.
- release of the 2.0 version of the Symfony bridge (by @mnapoli)
- PHP 5.5 or above is now required
- a lot of documentation improvements by 9 different contributors
- #389: exception message improvement by @mopahle
- #359, #411, #414, #412: compatibility with ProxyManager 1.* and 2.* (by @holtkamp and @mnapoli)
- #416: dumping definitions was refactored into a more lightweight and simple solution; definition "dumpers" have been removed (internal classes), definitions can now be cast to string directly (by @mnapoli)
Read the news entry.
Improvements:
- #347 (includes #333 and #345): by @jdreesen, @quimcalpe and @mnapoli
- Allow injection of any container object as factory parameter via type hinting
- Allow injection of a
DI\Factory\RequestedEntry
object to get the requested entry name
- #272: Support
"Class::method""
syntax for callables (by @jdreesen) - #332: IDE support (plugin and documentation) (by @pulyaevskiy, @avant1 and @mnapoli)
- #326: Exception messages are simpler and more consistent (by @mnapoli)
- #325: Add a "Edit this page" button in the website to encourage users to improve the documentation (by @jdreesen)
Bugfixes:
- #321: Allow factory definitions to reference arbitrary container entries as callables (by @jdreesen)
- #335: Class imports in traits are now considered when parsing annotations (by @thebigb)
Read the news entry.
Improvements:
- Zend Framework 2 integration (by @Rastusik)
- #308: Instantiate factories using the container (
DI\factory(['FooFactory', 'create'])
) - Many performances improvements - some benchmarks show up to 35% performance improvements, real results may vary of course
- Many documentation improvements (@jdreesen, @mindplay-dk, @mnapoli, @holtkamp, @Rastusik)
- #296: Provide a faster
ArrayCache
implementation, mostly useful in micro-benchmarks
Bugfixes:
- #257 & #274: Private properties of parent classes are not injected when using annotations
- #300: Exception if object definition extends an incompatible definition
- #306: Errors when using parameters passed by reference (fixed by @bradynpoulsen)
- #318:
Container::call()
ignores parameter's default value
Internal changes:
- #276: Tests now pass on Windows (@bgaillard)
This is the complete change log. You can also read the migration guide for upgrading, or the news article for a nicer introduction to this new version.
Improvements:
-
Moved to an organization on GitHub: github.com/PHP-DI/PHP-DI
-
The package has been renamed to: from
mnapoli/php-di
tophp-di/php-di
-
Lighter package: from 10 to 3 Composer dependencies!
-
#235:
DI\link()
is now deprecated in favor ofDI\get()
. There is no BC break asDI\link()
still works. -
#207: Support for
DI\link()
in arrays -
#203: New
DI\string()
helper (documentation) -
#208: Support for nested definitions
-
#226:
DI\factory()
can now be omitted with closures:// before 'My\Class' => DI\factory(function () { ... }) // now (optional shortcut) 'My\Class' => function () { ... }
-
#193:
DI\object()->method()
now supports calling the same method twice (or more). -
#248: New
DI\decorate()
helper to decorate a previously defined entry (documentation) -
#215: New
DI\add()
helper to add entries to an existing array (documentation) -
#218:
ContainerBuilder::addDefinitions()
can now take an array of definitions -
#211:
ContainerBuilder::addDefinitions()
is now fluent (return$this
) -
#250:
Container::call()
now also accepts parameters not indexed by name as well as embedded definitions (documentation) -
Various performance improvements, e.g. lower the number of files loaded, simpler architecture, …
BC breaks:
- PHP-DI now requires a version of PHP >= 5.4.0
- The package is lighter by default:
- #251: Annotations are disabled by default, if you use annotations enable them with
$containerBuilder->useAnnotations(true)
. Additionally thedoctrine/annotations
package isn't required by default anymore, so you also need to runcomposer require doctrine/annotations
. doctrine/cache
is not installed by default anymore, you need to require it incomposer.json
(~1.0
) if you want to configure a cache for PHP-DI- #198:
ocramius/proxy-manager
is not installed by default anymore, you need to require it incomposer.json
(~1.0
) if you want to use lazy injection
- #251: Annotations are disabled by default, if you use annotations enable them with
- Closures are now converted into factory definitions automatically. If you ever defined a closure as a value (e.g. to have the closure injected in a class), you need to wrap the closure with the new
DI\value()
helper. - #223:
DI\ContainerInterface
was deprecated since v4.1 and has been removed
Internal changes in case you were replacing/extending some parts:
- the definition sources architecture has been refactored, if you defined custom definition sources you will need to update your code (it should be much easier now)
- #252:
DI\Scope
internal implementation has changed. You are encouraged to use the constants (DI\Scope::SINGLETON
andDI\Scope::PROTOTYPE
) instead of the static methods, but backward compatibility is kept (static methods still work). - #241:
Container::call()
now uses the Invoker external library
Read the news entry.
- #185 Support for invokable objects in
Container::call()
- #192 Support for invokable classes in
Container::call()
(will instantiate the class) - #184 Option to ignore phpdoc errors
Read the news entry.
- #176 New definition type for reading environment variables:
DI\env()
- #181
DI\FactoryInterface
andDI\InvokerInterface
are now auto-registered inside the container so that you can inject them without any configuration needed - #173
$container->call(['MyClass', 'method]);
will getMyClass
from the container ifmethod()
is not a static method
- Fixed #180:
Container::call()
with object methods ([$object, 'method']
) is now supported
- Support for PHP 5.3.3, which was previously incomplete because of a bug in the reflection (there is now a workaround for this bug)
But if you can, seriously avoid this (really old) PHP version and upgrade.
Read the news entry.
Minor BC-break: Optional parameters (that were not configured) were injected, they are now ignored, which is what naturally makes sense since they are optional. Example:
public function __construct(Bar $bar = null)
{
$this->bar = $bar ?: $this->createDefaultBar();
}
Before 4.2, PHP-DI would try to inject a Bar
instance. From 4.2 and onwards, it will inject null
.
Of course, you can still explicitly define an injection for the optional parameters and that will work.
All changes:
- #162 Added
Container::call()
to call functions with dependency injection - #156 Wildcards (
*
) in definitions - #164 Prototype scope is now available for
factory()
definitions too - FIXED #168
Container::has()
now returns false for interfaces and abstract classes that are not mapped in the definitions - FIXED #171 Optional parameters are now ignored (not injected) if not set in the definitions (see the BC-break warning above)
Read the news entry.
BC-breaks: None.
- #138 Container-interop compliance
- #143 Much more explicit exception messages
- #157 HHVM support
- #158 Improved the documentation for Symfony 2 integration
Major changes:
- The configuration format has changed (read more here to understand why)
Read the migration guide if you are using 3.x: Migration guide from 3.x to 4.0.
BC-breaks:
- YAML, XML and JSON definitions have been removed, and the PHP definition format has changed (see above)
ContainerSingleton
has been removed- You cannot configure an injection as lazy anymore, you can only configure a container entry as lazy
- The Container constructor now takes mandatory parameters. Use the ContainerBuilder to create a Container.
- Removed
ContainerBuilder::setDefinitionsValidation()
(no definition validation anymore) ContainerBuilder::useReflection()
is now named:ContainerBuilder::useAutowiring()
ContainerBuilder::addDefinitionsFromFile()
is now named:ContainerBuilder::addDefinitions()
- The
$proxy
parameter inContainer::get($name, $proxy = true)
hase been removed. To get a proxy, you now need to define an entry as "lazy".
Other changes:
- Added
ContainerInterface
andFactoryInterface
, both implemented by the container. - #115 Added
Container::has()
- #142 Added
Container::make()
to resolve an entry - #127 Added support for cases where PHP-DI is wrapped by another container (like Acclimate): PHP-DI can now use the wrapping container to perform injections
- #128 Configure entry aliases
- #110 XML definitions are not supported anymore
- #122 JSON definitions are not supported anymore
ContainerSingleton
has finally been removed- Added
ContainerBuilder::buildDevContainer()
to get started with a default container very easily. - #99 Fixed "
@param
with PHP internal type throws exception"
- FIXED #126:
Container::set
without effect if a value has already been set and retrieved
Read the news entry.
- Importing
@Inject
and@Injectable
annotations is now optional! It means that you don't have to writeuse DI\Annotation\Inject
anymore - FIXED #124:
@Injects
annotation conflicts with other annotations
Read the news entry.
- #106 You can now define arrays of values (in YAML, PHP, …) thanks to @unkind
- #98
ContainerBuilder
is now fluent thanks to @drdamour - #101 Optional parameters are now supported: if you don't define a value to inject, their default value will be used
- XML definitions have been deprecated, there weren't even documented and were not maintained. They will be removed in 4.0.
- FIXED #100: bug for lazy injection in constructors
Read the news entry.
- Inject dependencies on an existing instance with
Container::injectOn
(work from Jeff Flitton: #89). - #86: Optimized definition lookup (faster)
- FIXED #87: Rare bug in the
PhpDocParser
, fixed by drdamour
Read the news entry.
Small BC-break: PHP-DI 3.0 and 3.1 injected properties before calling the constructor. This was confusing and not supported for internal classes. From 3.2 and on, properties are injected after calling the constructor.
- Lazy injection: it is now possible to use lazy injection on properties and methods (setters and constructors).
- Lazy dependencies are now proxies that extend the class they proxy, so type-hinting works.
- Addition of the
ContainerBuilder
object, that helps to create and configure aContainer
. - Some methods for configuring the Container have gone deprecated in favor of the
ContainerBuilder
. Fear not, these deprecated methods will remain until next major version (4.0).Container::useReflection
, use ContainerBuilder::useReflection insteadContainer::useAnnotations
, use ContainerBuilder::useAnnotations insteadContainer::setDefinitionCache
, use ContainerBuilder::setDefinitionCache insteadContainer::setDefinitionsValidation
, use ContainerBuilder::setDefinitionsValidation instead
- The container is now auto-registered (as 'DI\Container'). You can now inject the container without registering it.
- Value definitions (
$container->set('foo', 80)
) are not cached anymore - FIXED #82: Serialization error when using a cache
Read the news entry.
- Zend Framework 1 integration through the PHP-DI-ZF1 project
- Fixed the order of priorities when you mix different definition sources (reflection, annotations, files, …). See Definition overriding
- Now possible to define null values with
$container->set('foo', null)
(see #79). - Deprecated usage of
ContainerSingleton
, will be removed in next major version (4.0)
- FIXED #76: Definition conflict when setting a closure for a class name
- FIXED #70: Definition conflict when setting a value for a class name
- FIXED #69: YamlDefinitionFileLoader crashes if YAML file is empty
- Fixed over-restrictive dependencies in composer.json
- #64: Non PHP-DI exceptions are not captured-rethrown anymore when injecting dependencies (cleaner stack trace)
- #62: When using aliases, definitions are now merged
Major compatibility breaks with 2.x.
- The container is no longer a Singleton (but
ContainerSingleton::getInstance()
is available for fools who like it) - Setter injection
- Constructor injection
- Scopes: singleton (share the same instance of the class) or prototype (create a new instance each time it is fetched). Defined at class level.
- Configuration is reworked from scratch. Now every configuration backend can do 100% of the job.
- Provided configuration backends:
- Reflection
- Annotations: @Inject, @Injectable
- PHP code (
Container::set()
) - PHP array
- YAML file
- As a consequence, annotations are not mandatory anymore, all functionalities can be used with or without annotations.
- Renamed
DI\Annotations\
toDI\Annotation\
Container
no longer implements ArrayAccess, use only$container->get($key)
now- ZF1 integration broken and removed (work in progress for next releases)
- Code now follows PSR1 and PSR2 coding styles
- FIXED: #58 Getting a proxy of an alias didn't work
use
statements to import classes from other namespaces are now taken into account with the@var
annotation- Updated and lightened the dependencies :
doctrine/common
has been replaced with more specificdoctrine/annotations
anddoctrine/cache
Major compatibility breaks with 1.x.
Container::resolveDependencies()
has been renamed toContainer::injectAll()
- Dependencies are now injected before the constructor is called, and thus are available in the constructor
- Merged
@Value
annotation with@Inject
: no difference between value and bean injection anymore - Container implements ArrayAccess for get() and set() (
$container['db.host'] = 'localhost';
) - Ini configuration files removed: configuration is done in PHP
- Allow to define beans within closures for lazy-loading
- Switched to MIT License
Warning:
- If you use PHP 5.3 and __wakeup() methods, they will be called when PHP-DI creates new instances of those classes.
- Caching of annotations based on Doctrine caches
- DependencyManager renamed to Container
- Refactored basic Container usage with
get
andset
- Allow named injection
@Inject(name="")
- Zend Framework integration