Issue Bug Closed

#93911Cannot strong-type properties when extending a domain model

Xavier Perseguers 04/12/21, 22:29 updated 03/23/25, 22:00

Description

In the context of using Configuration/Extbase/Persistence/Classes.php, Extbase allows to extend a domain model, e.g.:

namespace VENDOR\MyExt\Domain\Model;

class User extends FrontendUser
{

    /**
     * @var string
     */
    protected $courseStudy;

    // getter/setter

Corresponding definition looks like:

return [
    \TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class => [
        'tableName' => 'fe_users',
        'recordType' => '\TYPO3\CMS\Extbase\Domain\Model\FrontendUser',
        'subclasses' => [
            'Tx_MyExt_User' => \VENDOR\MyExt\Domain\Model\User::class,
        ]
    ],
    \VENDOR\MyExt\Domain\Model\User::class => [
        'tableName' => 'fe_users',
        'properties' => [
            'courseStudy' => [
                'fieldName' => 'tx_myext_course_study'
            ],
        ],
    ],
];

This way the mapping just works fine!

However if you strong type the property, then it doesn’t work anymore:

namespace VENDOR\MyExt\Domain\Model;

class User extends FrontendUser
{

    /**
     * @var string
     */
    protected string $courseStudy;

    // getter/setter

(mind the protected string).

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 5

  1. Guido Schmechel commented 09/09/21, 18:30

    We also had problems with the extbase persistence classes today. It was not so much the string notation, but we were still missing a TCA passthrough to the name of the original field.

  2. Christian Kuhn commented and changed status New Closed 02/19/22, 09:43

    This does not work on PHP level. This is invalid:

    class foo {
        protected $foo;
    }
    class bar extends foo {
        protected string $foo;
    }

    Quoting https://wiki.php.net/rfc/typed_properties_v2:

    “Property types are invariant. This means that the type of a (non-private) property is not allowed to change during inheritance (this includes adding or removing property types). If the parent property is private, then the type may be changed arbitrarily.
    […]
    As such, neither contravariance nor covariance are applicable to property types.
    ”

  3. Xavier Perseguers commented and changed status Closed New 02/19/22, 09:48

    This was not about overriding an existing property but adding another property.

  4. Benni Mack commented and changed status New Needs Feedback 06/20/24, 13:13

    Xavier Perseguers wrote in #note-3:
    > This was not about overriding an existing property but adding another property.

    Hey Xavier,

    this works for me in v12 installation (extending EXT:news). Does it work for you in v11+ ?

  5. Benni Mack commented and changed status Needs Feedback Closed 03/23/25, 22:00

    Closing this issue due to lack of feedback after 90 days. If you feel this is the wrong decision, let me know, so I will re-open the issue.