Skip to content

Constructor property promotion - part 1 #16087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,9 @@ the same key could be invalidated with one function call::

class SomeClass
{
private $myCachePool;

// using autowiring to inject the cache pool
public function __construct(TagAwareCacheInterface $myCachePool)
public function __construct(private TagAwareCacheInterface $myCachePool)
{
$this->myCachePool = $myCachePool;
}

public function someMethod()
Expand Down
5 changes: 1 addition & 4 deletions components/console/logger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ PSR-3 compliant logger::

class MyDependency
{
private $logger;

public function __construct(LoggerInterface $logger)
public function __construct(private LoggerInterface $logger)
{
$this->logger = $logger;
}

public function doStuff()
Expand Down
10 changes: 2 additions & 8 deletions components/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ so this is passed into the constructor::

class Mailer
{
private $transport;

public function __construct($transport)
public function __construct(private $transport)
{
$this->transport = $transport;
}

// ...
Expand Down Expand Up @@ -99,11 +96,8 @@ like this::

class NewsletterManager
{
private $mailer;

public function __construct(\Mailer $mailer)
public function __construct(private \Mailer $mailer)
{
$this->mailer = $mailer;
}

// ...
Expand Down
5 changes: 1 addition & 4 deletions components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,8 @@ order. Start by creating this custom event class and documenting it::
{
public const NAME = 'order.placed';

protected $order;

public function __construct(Order $order)
public function __construct(protected Order $order)
{
$this->order = $order;
}

public function getOrder(): Order
Expand Down
5 changes: 1 addition & 4 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,

class Mailer
{
protected $options;

public function __construct(array $options = [])
public function __construct(protected array $options = [])
{
$this->options = $options;
}
}

Expand Down
7 changes: 1 addition & 6 deletions components/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,8 @@ is added in a new class implementing :class:`Symfony\\Component\\Runtime\\Runner

class ReactPHPRunner implements RunnerInterface
{
private $application;
private $port;

public function __construct(RequestHandlerInterface $application, int $port)
public function __construct(private RequestHandlerInterface $application, private int $port)
{
$this->application = $application;
$this->port = $port;
}

public function run(): int
Expand Down
13 changes: 2 additions & 11 deletions form/validation_group_service_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ parameter::

class ValidationGroupResolver
{
private $service1;

private $service2;

public function __construct($service1, $service2)
public function __construct(private $service1, private $service2)
{
$this->service1 = $service1;
$this->service2 = $service2;
}

public function __invoke(FormInterface $form): array
Expand All @@ -44,11 +38,8 @@ Then in your form, inject the resolver and set it as the ``validation_groups``::

class MyClassType extends AbstractType
{
private $groupResolver;

public function __construct(ValidationGroupResolver $groupResolver)
public function __construct(private ValidationGroupResolver $groupResolver)
{
$this->groupResolver = $groupResolver;
}

// ...
Expand Down
5 changes: 1 addition & 4 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ serialized::

class SmsNotification
{
private $content;

public function __construct(string $content)
public function __construct(private string $content)
{
$this->content = $content;
}

public function getContent(): string
Expand Down
12 changes: 4 additions & 8 deletions serializer/custom_normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:

class TopicNormalizer implements ContextAwareNormalizerInterface
{
private $router;
private $normalizer;

public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer)
{
$this->router = $router;
$this->normalizer = $normalizer;
}
public function __construct(
private UrlGeneratorInterface $router,
private ObjectNormalizer $normalizer
) {}

public function normalize($topic, string $format = null, array $context = [])
{
Expand Down
5 changes: 1 addition & 4 deletions session/locale_sticky_session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ correct locale however you want::

class LocaleSubscriber implements EventSubscriberInterface
{
private $defaultLocale;

public function __construct(string $defaultLocale = 'en')
public function __construct(private string $defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}

public function onKernelRequest(RequestEvent $event)
Expand Down