Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite08dc4c

Browse files
committed
minor#18070 [DependencyInjection] Add theconstructor option to service definition (alexandre-daubois)
This PR was merged into the 6.3 branch.Discussion----------[DependencyInjection] Add the `constructor` option to service definitionRelated tosymfony/symfony#49665Commits-------6040cfe [DependencyInjection] Add the `constructor` option to service definition
2 parents3696276 +6040cfe commite08dc4c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

‎service_container/factories.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,77 @@ You can omit the class on the factory declaration:
164164
->factory([null, 'create']);
165165
};
166166
167+
It is also possible to use the ``constructor`` option, instead of passing ``null``
168+
as the factory class:
169+
170+
..configuration-block::
171+
172+
..code-block::php-attributes
173+
174+
// src/Email/NewsletterManager.php
175+
namespace App\Email;
176+
177+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
178+
179+
#[Autoconfigure(bind: ['$sender' => 'fabien@symfony.com'], constructor: 'create')]
180+
class NewsletterManager
181+
{
182+
private string $sender;
183+
184+
public static function create(string $sender): self
185+
{
186+
$newsletterManager = new self();
187+
$newsletterManager->sender = $sender;
188+
// ...
189+
190+
return $newsletterManager;
191+
}
192+
}
193+
194+
..code-block::yaml
195+
196+
# config/services.yaml
197+
services:
198+
# ...
199+
200+
App\Email\NewsletterManager:
201+
constructor:'create'
202+
arguments:
203+
$sender:'fabien@symfony.com'
204+
205+
..code-block::xml
206+
207+
<!-- config/services.xml-->
208+
<?xml version="1.0" encoding="UTF-8" ?>
209+
<containerxmlns="http://symfony.com/schema/dic/services"
210+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
211+
xsi:schemaLocation="http://symfony.com/schema/dic/services
212+
https://symfony.com/schema/dic/services/services-1.0.xsd">
213+
214+
<services>
215+
<serviceid="App\Email\NewsletterManager"constructor="create">
216+
</service>
217+
</services>
218+
</container>
219+
220+
..code-block::php
221+
222+
// config/services.php
223+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
224+
225+
use App\Email\NewsletterManager;
226+
227+
return function(ContainerConfigurator $containerConfigurator) {
228+
$services = $containerConfigurator->services();
229+
230+
$services->set(NewsletterManager::class)
231+
->constructor('create');
232+
};
233+
234+
..versionadded::6.3
235+
236+
The ``constructor`` option was introduced in Symfony 6.3.
237+
167238
Non-Static Factories
168239
--------------------
169240

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp