Can anyone explain what is \Magento\Framework\DataObject do? I use it but I don't understand its core purpose Or which tables it deals with.

2 Answers 2


Magento\Framework\DataObject is the equivalent of Varien_Object from magento 1.
To put it short, it is a glorified array.
Long version:
It is basically a wrapper over an array that allows you to set/get/unset values in that array in a OOP way.
It also offers other shortcuts.
For example $array['missing'] will throw an exception if the key 'missing' is missing.
using the DataObject you can call $obj->getMissing() or $obj->getData('missing') will return null if the missing key is missing in the array.
One other useful thing about it is that objects are passed by reference and arrays are passed by value.
So if you pass a DataObject as a parameter to a method and you change it inside the method, it will stay changed when the method is done.

This class does not deal with tables, but most of the models that deal with tables extend DataObject.


DataObject is Universal data container with array access implementation. it holds the data for easy manipulations. and it is implemented by ArrayAccess. it contains getters/setters. This is constructor code.

 * Constructor
 * By default is looking for first argument as array and assigns it as object attributes
 * This behavior may change in child classes
 * @param array $data
public function __construct(array $data = [])
    $this->_data = $data;

it holds the data while creating object. you can create by empty data using addData(..) you can append data.

some of useful methods
public function toArray(array $keys = [])
public function toXml(...)
public function convertToJson(array $keys = [])
Using DataObject you can use callback functions
public function serialize(...)

Usage examples:

$types[$type] = new \Magento\Framework\DataObject(
                'id' => $type,
                'cache_type' => $node['label'],
                'description' => $node['description'],
                'tags' => $typeTags,
                'status' => (int)$this->_cacheState->isEnabled($type),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.