#106565Constructor Promotion Not Supported in Extbase Domain Models
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
-
Gerrit Code Review pushed patch set 1 for main to change 89095 status
NewUnder Review <span data-processor="relativetime" data-value="2025-04-13T10:08:25+00:00" > 04/13/25, 10:08 </span> -
Lina Wolf changed description <span data-processor="relativetime" data-value="2025-04-13T10:12:09+00:00" > 04/13/25, 10:12 </span>
-
Gerrit Code Review pushed patch set 2 for main to change 89095 <span data-processor="relativetime" data-value="2025-04-18T16:14:07+00:00" > 04/18/25, 16:14 </span>
-
Gerrit Code Review pushed patch set 1 for 13.4 to change 89174 <span data-processor="relativetime" data-value="2025-04-19T10:45:45+00:00" > 04/19/25, 10:45 </span>
-
Lina Wolf commented and changed status
Under ReviewResolved done0 %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.
-
Benni Mack changed status
ResolvedClosed <span data-processor="relativetime" data-value="2025-12-02T14:22:36+00:00" > 12/02/25, 14:22 </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:
initializeObject()when hydrating to allow setting properties to default values. (documented)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:
is not a 1:1 replacement for the class property:
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:
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).