Attention

This manual is no longer being maintained for TYPO3 versions 11.5 and above. The majority of the content has been migrated to the Extbase or Fluid sections in "TYPO3 Explained".

Create The Domain Model

The domain of our first extension is very simple. The essential concept of our domain is the "product". All important properties of a product and its behavior are defined in the model MyVendor\StoreInventory\Domain\Model\Product which is stored in a file with the name Product.php. The name of the file corresponds to the name of the model. The file itself is stored in the folder EXT:store_inventory/Classes/Domain/Model/.

Tip

The class names must reflect the folder structure to ensure that class loading is working. For example the class MyVendor\MyExtension\FirstFolder\SecondFolder\File should be in the folder my_extension/Classes/FirstFolder/SecondFolder/File.php. Pay attention to the corresponding upper casing of the folder names.

Let's take a deeper look at the model \MyVendor\StoreInventory\Domain\Model\Product.

The model Product in our extension denotes the aggregate root and represents an entity (as opposed to a ValueObject) and should extend \TYPO3\CMS\Extbase\DomainObject\AbstractEntity.

File: Classes/Domain/Model/Product.php
<?php

namespace MyVendor\StoreInventory\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Product extends AbstractEntity
{
    protected $name = '';

    protected $description = '';

    protected $quantity = 0;

    public function __construct(string $name = '', string $description = '', int $quantity = 0): void
    {
        $this->setName($name);
        $this->setDescription($description);
        $this->setQuantity($quantity);
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setDescription(string $description): void
    {
        $this->description = $description;
    }

    public function getDescription(): string
    {
        return $this->description;
    }

    public function setQuantity(int $quantity): void
    {
        $this->quantity = $quantity;
    }

    public function getQuantity(): int
    {
        return $this->quantity;
    }
}

The product properties are designed as properties $name, $description and $quantity and protected (encapsulated) against direct access from outside by using the keyword protected. The property values can be set and/or read only by the corresponding getters and setters.

Tip

At first glance, the methods may appear to be cumbersome for accessing the class variables. However, they have several advantages: The internals of processing can be added or changed at a later time, without needing to make changes to the calling object. Additionally, fire-grained access control is possible as for example the reading can be permitted, without simultaneously allowing writing access. Most modern IDEs are able to create getters and setters automatically.

The method __construct() serves to guarantee a well defined state at the beginning of the life cycle of the object. The properties of the product are set with their respectively default values.