Use final to define classes that are NOT extension points

Created on 10 December 2018, over 5 years ago
Updated 3 June 2024, 27 days ago

Problem/Motivation

In core we've held off from using final on classes because of the idea that final limits extensibility and a key feature is extensibility.

However there are cases where we've defined specific types of classes in core as not being extensible and @internal by default. These include:

  • Plugins
  • Events
  • Forms
  • Controllers
  • Paramconvertors
  • Access policies (already marked as final)

See https://www.drupal.org/core/d8-bc-policy β†’

However people are extending these in contrib and custom code and then when we make changes to the constructors things break. For example:
https://www.drupal.org/project/drupal/issues/2594425 β†’ and then https://www.drupal.org/project/menu_block/issues/2968049 β†’

Proposed resolution

Use final instead of an implicit @internal. Put in another way we need to prioritise composition over inheritance. So a plugin that wants to borrow functionality from another plugin could create that plugin in it's constructor and then use that object. See #2968049-18: Dependency Injection issue β†’ as an example.

Making this change is likely to shift some costs from future maintenance to the here-and-now but in the long run it means that core upgrades to major and minor releases can be done with more confidence because our conventions have moved from wishful thinking to language constructs.

Remaining tasks

  • Bring in the technical working group as this is likely to involve coding standards.
  • Build core consensus - this is likely to be tricky because there is an idea that the use of final is bad.

User interface changes

None

API changes

None :) everything is already implicitly @internal

Data model changes

None

Release notes snippet

This is Drupal 9 only change.

🌱 Plan
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024