Skip to content

Commit ebc1b7b

Browse files
linawolfsbuerk
andauthored
[TASK] Typed vs Untyped properties in Extbase (#5532)
* [TASK] Typed vs Untyped properties in Extbase Releases: main, 13.4, 12.4 * [TASK] Typed vs Untyped properties in Extbase Releases: main, 13.4, 12.4 * Apply suggestions from code review Co-authored-by: Stefan Bürk <[email protected]> --------- Co-authored-by: Stefan Bürk <[email protected]>
1 parent 08615d2 commit ebc1b7b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Model/Index.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ however get displayed when explicitly called:
9494
But it is there:
9595
<f:debug>{post.info.combinedString}</f:debug>
9696

97+
.. _extbase-model-properties-untyped:
98+
99+
Typed vs. untyped properties in Extbase models
100+
==============================================
101+
102+
In Extbase, you can define model properties using either PHP native type
103+
declarations or traditional `@var` annotations. Typed properties are
104+
preferred, untyped properties are still supported for backward compatibility.
105+
106+
The example below demonstrates a basic model with both a typed and an untyped
107+
property:
108+
109+
.. literalinclude:: _codesnippets/_untypedProperties.php
110+
:caption: EXT:my_extension/Classes/Domain/Model/Blog.php
111+
112+
- `$title` is a *typed property*, using PHP’s type declaration. This is the
113+
recommended approach as it enforces type safety and improves code
114+
readability.
115+
116+
- `$published` is an *untyped property*, defined only with a docblock. This
117+
remains valid and is often used in older codebases.
118+
119+
For persisted properties (those stored in the database), ensure that the
120+
property type matches the corresponding
121+
`TCA Field type <https://docs.typo3.org/permalink/t3tca:columns-types>`_
122+
to avoid data mapping errors.
123+
124+
Nullable and `Union types <https://docs.typo3.org/permalink/t3coreapi:extbase-model-properties-union-types>`_
125+
are also supported.
126+
127+
.. note:: Typed properties are strongly encouraged in all new TYPO3 extensions.
128+
97129
.. _extbase-model-properties-default-values:
98130

99131
Default values for model properties
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vendor\Extension\Domain\Model;
6+
7+
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
8+
9+
class Blog extends AbstractEntity
10+
{
11+
/**
12+
* Typed property (preferred)
13+
*
14+
* @var string
15+
*/
16+
protected string $title = '';
17+
18+
/**
19+
* Untyped property (legacy-compatible)
20+
*
21+
* @var bool
22+
*/
23+
protected $published = false;
24+
25+
// Getters and Setters
26+
}

0 commit comments

Comments
 (0)