Issue Bug Closed

#106565Constructor Promotion Not Supported in Extbase Domain Models

Lina Wolf 04/13/25, 09:45 updated 12/02/25, 14:22

Description

When using PHP 8+ constructor property promotion in Extbase domain models (classes extending \TYPO3\CMS\Extbase\DomainObject\AbstractEntity), the promoted properties are not persisted, hydrated, or validated as expected.

Although promoted properties are real class properties (per PHP), Extbase does not treat them equivalently to explicitly declared properties. This leads to unexpected behavior when persisting or retrieving entities from the database.

When using:

class Blog extends AbstractEntity
{
    public function __construct(
        #[Lazy]
        #[Cascade(['value' => 'remove'])]
        public ObjectStorage $posts = new ObjectStorage()
    ) {}
}

The behaviour is not the same like for

class Blog extends AbstractEntity
{
    #[Lazy]
    #[Cascade(['value' => 'remove'])]
    protected ObjectStorage $posts;

    public function __construct()
    {
        $this->posts = new ObjectStorage();
    }

    public function getPosts(): ObjectStorage
    {
        return $this->posts;
    }
}

When I write a test in which I change typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/Blog.php
To constructor promotion there are currently 12 errors and 1 failute in the functional extbase tests.

Reviews 2

History 47

  1. Stefan Bürk commented <span data-processor="relativetime" data-value="2025-04-18T16:42:57+00:00" > 04/18/25, 16:42 </span>

    To make it short: This is not a bug and Extbase is not broken here.

    For extbase models following things are a long standing known fact,
    which is also documented:

    • Extbase instantiates models without calling the constructor when hydrating. (documented)
    • Extbase calls initializeObject() when hydrating to allow setting properties to default values. (documented)
    • Extbase tries to hydrate all properties given in the data array, not touching or setting other properties (except some system ones).
    • Implementor/Developer creating the extbase model is in charge to ensure to have either correctly initialized properites for direct creation or hydrating by extbase, or handle it correctly in getter methods.

    PHP capabilities and language changed over the time Exbase is beeing around, which does not
    change the concet how extbase works in any regard. It’s still the responsibility of the person
    implementing the model to ensure correct initialized properties.

    The “bug” here is a missunderstanding on how PHP works, special for the constructor parameter promotion.

    Simple said:

    public function __construct(protected ?string $nullableStringProperty = null) {}

    is not a 1:1 replacement for the class property:

    class someClass {
      protected ?string $nullableStringProperty = null;
    
      public function __construct() {}
    }

    The point is, that PHP declares the property WITHOUT the default value assignment,
    if the constructore method is called but executed the default value assignment only
    when the constructor is called, which would be basically following code:

    class someClass {
      protected ?string $nullableStringProperty;
    
      public function __construct(
        ?string $nullableStringProperty = null
      ) {
        $this->nullableStringProperty = $nullableStringProperty;
      }
    }

    The latter example holds a uninitialized property when the constructor is not called,
    and requires to do the “default” value assignment using `initializeObject()` to have
    it initialized when Extbase ORM hydrates the model. Following that, the same counts
    when using CPP.

    Note, that this is true since PHP 7.4 when typed class properties got to play role,
    and required to have either the default value assigned in the property definition
    or set within the constructor (when the constructor is called).

    The above explanation shows that this is not a bug, and more a thing for the documentation
    around extbase and extbase models to make not so experienced PHP developers (has nothing to
    do with TYPO3 or extbase) aware how to correctly deal with that.

    I took the oppurtunity and transformed your “POC showing broking extbase” gerrit change into
    a “Demonstrate how it is done correctly” as a task along with tests, thus not closing this
    issue here now.

    As you are part of the documentation team, can you coordinate the modification and enhancement
    of the documentation for this ? Let me know if more insight in PHP is required here, when shareing
    the technical background is required or if I should do a short huddle/screenshare to make that a
    quick demosntartion over the versions and behaviour (completly with plain PHP without TYPO3 or extbase).

  2. Lina Wolf commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2025-04-19T11:05:10+00:00" > 04/19/25, 11:05 </span>

    Applied in changeset commit:688a261c657766853c7a81e250798d3984dc519a.