16

In magento 1 we can create varien object like this

$collection = new Varien_Data_Collection(); 
$varienObject = new Varien_Object();
$varienObject->setData($data);
$varienObject->setItem($item);
$collection->addItem($varienObject);

How to create object in magento 2?

32

In Magento 2 the Varien_Object equivalent is \Magento\Framework\DataObject. The class name was changed from Object to DataObject because object is a reserved word in PHP 7. So you could use something like:

$obj = new \Magento\Framework\DataObject();
$obj->setItem($item);

Update 2018

This answer intended to illustrate an answer the original question in the most succinct way possible and not in the context of a real code example. Although it did answer the question, \Magento\Framework\DataObject is the new Varien_Object, the implementation wasn't 100% in line with Magento 2 best practice. As @MatthiasKleine pointed out, Magento 2 best practice to create objects in your code is to use Magentos DI framework to inject a factory into your class via the constructor and use that factory to create your object. With that in mind, using DI to create a DataObject in your own code should look something like this:

namespace My/Module;

class Example {
    private $objectFactory;

    public function __construct(
        \Magento\Framework\DataObjectFactory $objectFactory
    ) {
        $this->objectFactory = $objectFactory;
        parent::__construct();
    }

    public function doSomething($item)
    {
        $obj = $this->objectFactory->create();
        $obj->setData('item', $item);
        //or
        $obj->setItem($item);
    }
}
  • 1
    This throws a warning when running 'phpcs --standard=MEQP2': Direct object instantiation (object of \Magento\Framework\DataObject) is discouraged in Magento 2. – Jeroen Vermeulen - MageHost Jul 28 '17 at 22:14
  • 1
    @JeroenVermeulen-MageHost right, but it's just a warning, to make you aware of a potential error. For the DataObject, I'd argue that there is no benefit in dependency injection and factories. It's just a glorified array after all. – Fabian Schmengler Jul 31 '17 at 17:13
  • How to assign multiple array value in the object? @Andrew – Dhaduk Mitesh Apr 18 '18 at 6:58
  • It will be better to use $obj->setData($item) instead of $obj->setItem($item) This way you will be able to get any field value of $obj using method directly. Like $obj->getName() etc. – Patrick W Sep 21 '18 at 5:48
13

Instead of creating the object with 'new' you should use DI (Dependency Injection) to inject the Factory class and use that factory to create new instances of DataObjects.

/**
 * @var \Magento\Framework\DataObjectFactory
 */
private $dataObjectFactory;

public function __construct(
    // ...
    \Magento\Framework\DataObjectFactory $dataObjectFactory
) {
    parent::__construct();

    $this->dataObjectFactory = $dataObjectFactory;
}

public function yourCode()
{
    $dataObject = $this->dataObjectFactory->create();
}
  • 1
    This way is correct way for declaring data object, when you follow coding standard like MEQP/MEQP2/PSR etc... Thanks... – Bhavik Dec 1 '17 at 10:41
  • Thank a lot -): It works like charm for me. – Charvi Parikh Mar 8 '18 at 12:30
  • How to assign multiple array value in the object? @Matthias – Dhaduk Mitesh Apr 18 '18 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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