[meta] Use property hooks to instantiate services from service closures

Created on 4 September 2025, about 1 month ago

Problem/Motivation

As the use of service closures becomes more widely adopted in order load services lazily, there is a concern about how to make service closure properties type safe.

One proposed way to do this is to use property hooks starting with Drupal 12, once PHP 8.4 becomes the minimum requirement. For example:

class NodeThemeHooks {

  protected FormBuilderInterface $formBuilder {
    get => ($this->formBuilderClosure)();
  }

  public function __construct(
    protected readonly RouteMatchInterface $routeMatch,
    protected readonly RendererInterface $renderer,
    protected readonly EntityTypeManagerInterface $entityTypeManager,
    #[AutowireServiceClosure('form_builder')]
    protected readonly \Closure $formBuilderClosure,
  ) { }

}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component

base system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024