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

Appearance settings

Commit 95a06a1

Browse filesBrowse files
committed
[DependencyInjection] Explain how to use the class itself as factory
1 parent defdea4 commit 95a06a1
Copy full SHA for 95a06a1

File tree

1 file changed

+73
-0
lines changed
Filter options

1 file changed

+73
-0
lines changed

‎service_container/factories.rst

Copy file name to clipboardExpand all lines: service_container/factories.rst
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,79 @@ create its object:
100100
the configured class name may be used by compiler passes and therefore
101101
should be set to a sensible value.
102102

103+
Using the Class as Factory Itself
104+
---------------------------------
105+
106+
When the static factory method is on the same class as the created instance,
107+
the class name can be omitted from the factory declaration.
108+
Let's suppose the ``NewsletterManager`` class has a ``create()`` method that needs
109+
to be called to create the object and needs a sender::
110+
111+
// src/Email/NewsletterManager.php
112+
namespace App\Email;
113+
114+
// ...
115+
116+
class NewsletterManager
117+
{
118+
private string $sender;
119+
120+
public static function create(string $sender): self
121+
{
122+
$newsletterManager = new self();
123+
$newsletterManager->sender = $sender;
124+
// ...
125+
126+
return $newsletterManager;
127+
}
128+
}
129+
130+
You can omit the class on the factory declaration:
131+
132+
.. configuration-block::
133+
134+
.. code-block:: yaml
135+
136+
# config/services.yaml
137+
services:
138+
# ...
139+
140+
App\Email\NewsletterManager:
141+
factory: [null, 'create']
142+
arguments:
143+
$sender: 'fabien@symfony.com'
144+
145+
.. code-block:: xml
146+
147+
<!-- config/services.xml -->
148+
<?xml version="1.0" encoding="UTF-8" ?>
149+
<container xmlns="http://symfony.com/schema/dic/services"
150+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
151+
xsi:schemaLocation="http://symfony.com/schema/dic/services
152+
https://symfony.com/schema/dic/services/services-1.0.xsd">
153+
154+
<services>
155+
<service id="App\Email\NewsletterManager">
156+
<factory method="create"/>
157+
</service>
158+
</services>
159+
</container>
160+
161+
.. code-block:: php
162+
163+
// config/services.php
164+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
165+
166+
use App\Email\NewsletterManager;
167+
168+
return function(ContainerConfigurator $containerConfigurator) {
169+
$services = $containerConfigurator->services();
170+
171+
// Note that we are not using service()
172+
$services->set(NewsletterManager::class)
173+
->factory([null, 'create']);
174+
};
175+
103176
Non-Static Factories
104177
--------------------
105178

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.