O
O
Outsider V.2021-07-27 13:59:48
PHP
Outsider V., 2021-07-27 13:59:48

Which implementation of DataObject is better?

In Magento 2 I see that all DataObjects inherit \Magento\Framework\DataObject:

/**
* Universal data container with array access implementation
*
* @api
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @since 100.0.2
*/
class DataObject implements \ArrayAccess


But this class has a lot of unnecessary methods (when you only need getters and setters), incl. magical - this time. And secondly, what is the best way to implement getters and setters through the data array property? Wouldn't it be better to just make private properties and get/set methods for them?

Below I sketched two options for implementing DTO (the second one is like magento, but without extra methods)

class PropsDto
{
    private string $strProp1;

    /**
     * @return string
     */
    public function getStrProp1(): string
    {
        return $this->strProp1;
    }

    /**
     * @param string $strProp1
     * @return PropsDto
     */
    public function setStrProp1(string $strProp1): self
    {
        $this->strProp1 = $strProp1;
        return $this;
    }

    /**
     * @return string
     */
    public function getStrProp2(): string
    {
        return $this->strProp2;
    }

    /**
     * @param string $strProp2
     * @return PropsDto
     */
    public function setStrProp2(string $strProp2): self
    {
        $this->strProp2 = $strProp2;
        return $this;
    }

    /**
     * @return string
     */
    public function getStrProp3(): string
    {
        return $this->strProp3;
    }

    /**
     * @param string $strProp3
     * @return PropsDto
     */
    public function setStrProp3(string $strProp3): self
    {
        $this->strProp3 = $strProp3;
        return $this;
    }
    private string $strProp2;
    private string $strProp3;
}

class ArrayDto
{
    private array $data = [];

    private const STR_PROP1 = "str_prop1";
    private const STR_PROP2 = "str_prop2";
    private const STR_PROP3 = "str_prop3";

    private function getData($key)
    {
        return $this->data[$key] ?? null;
    }

    public function setData($key, $value = null): self
    {
        $this->data[$key] = $value;

        return $this;
    }

    /**
     * Getter for StrProp1.
     *
     * @return string|null
     */
    public function getStrProp1(): ?string
    {
        return $this->getData(self::STR_PROP1);
    }

    /**
     * Setter for StrProp1.
     *
     * @param string|null $strProp1
     *
     * @return void
     */
    public function setStrProp1(?string $strProp1): self
    {
        return $this->setData(self::STR_PROP1, $strProp1);
    }

    /**
     * Getter for StrProp2.
     *
     * @return string|null
     */
    public function getStrProp2(): ?string
    {
        return $this->getData(self::STR_PROP2);
    }

    /**
     * Setter for StrProp2.
     *
     * @param string|null $strProp2
     *
     * @return void
     */
    public function setStrProp2(?string $strProp2): self
    {
        return $this->setData(self::STR_PROP2, $strProp2);
    }

    /**
     * Getter for StrProp3.
     *
     * @return string|null
     */
    public function getStrProp3(): ?string
    {
        return $this->getData(self::STR_PROP3);
    }

    /**
     * Setter for StrProp3.
     *
     * @param string|null $strProp3
     *
     * @return void
     */
    public function setStrProp3(?string $strProp3): self
    {
        return $this->setData(self::STR_PROP3, $strProp3);
    }
}


I ran it in a loop, and the first option even turned out to be 20 percent faster
. Can anyone give the advantages of implementing it through an array with values?
The ease of implementation of the toArray method and magic methods are no longer an argument, because we've been programming based on interfaces for a long time, not implementations. And for DTO in the same Magenta there are tools for converting to an array purely based on the analysis of the get methods of the interface.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Outsider V., 2021-07-29
@Audiophile

In general, the answer is that a similar implementation in Magento is needed for compatibility with its resource models. They take this array, filter it and figure it out in SQL. Those. in fact, this is for compatibility with obsolete code.
I did not find the advantages of others in such an implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question