- Issue created by @mparker17
- Merge request !48Issue #3518008: Deprecate passing extra parameters to __construct() to prepare for 3.0.x → (Open) created by mparker17
As the module evolves, we can expect new services to be added, service dependencies to change, etc.
The "classic" way to inject dependencies into a class is to use arguments to the constructor (i.e.: public function __construct($dependency1) { $this->dependency1 = $dependency1; }
. But if you want to add a new dependency, or delete a dependency, then you have to change the function signature of the constructor (which is often public).
Technically, Drupal core's backwards-compatibility policy considers service object constructors to be @internal → ; however, in practice, changing the signature of service object constructors can occasionally cause fatal errors after a module upgrade, which isn't ideal.
Fortunately, PHP provides an alternate way to inject dependencies that doesn't involve changing function signatures, and thus avoids fatal errors on module upgrades...
class Foo {
/* Omit the constructor, i.e.: to inherit it from its base class */
public static function create(ContainerInterface $container) {
$instance = new static():
$instance->dependency1 = $container->get('dependency1');
return $instance;
}
}
... to distinguish these from the classic-style of dependency injection, this issue will refer to this as the "new-style of dependency injection".
At time-of-writing, the 2.x
branch of structure_sync has 4 instances of classic-style dependency injection:
There is talk of creating a 3.0.x
branch in
✨
Drop support for D8, D9, D10.0-10.3
Active
.
We can't change constructor signatures in the 2.x
branch; but we can prepare to change the signatures in the 3.0.x
branch by deprecating the use of classic-style dependency injection.
For the 4 services listed above...
... this will allow us to remove those constructors in a 3.0.x branch.
None.
None.
None.
Active
2.0
Code