Blog

Data objects – What are they and what makes them special?

Data Objects

Table of Contents

Data objects are also known as Varien objects by Magento 1.x developers. What are they and what makes them special? Let us find out in this Magento tutorial.

Almost all Models and Blocks in Magento extend DataObject. And that allows them to do something pretty interesting. You might have seen, that we can call the set{Name}and get{Name}on all models, where the{Name}can be anything, in CamelCase. So that means if we create a class Personand extend the DataObject, we can essentially call getName()getAge()setName("Codilar")and setAge(24)on an object of the class person, even though we didn’t explicitly create these methods, and neither are they created in the parent class. Then how does it work?!?

Magic methods

To understand this, we need to first understand what magic methods are in PHP. Any function starting with __(2 underscores) is called a magic method. These magic methods allow you to “twist the OOPs flow a little bit” to serve your purpose. A few examples would be

The __call() magic method

The __call() magic method, if exists, will be called whenever we try to access a method which is either inaccessible (private or protected method) or non-existent. It basically gives you one last chance to “fix” the error, before throwing a “call to a non existent method” error. Let’s take an example to see how that works. Consider a class Person

<?php
/**
 *
 * @package     magento2
 * @author      Codilar Technologies
 * @license     https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
 * @link        https://www.codilar.com/
 */

class Person
{

}

Now if we create an object of this class, and try to call a non-existent method, like getName(), it will obviously throw an error

    $person = new Person();
    $person->getName(); //throws an error

Now if we change the Person class, and use the __call() magic method, we can “catch” this error

<?php
/**
 *
 * @package magento2
 * @author Codilar Technologies
 * @license https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
 * @link https://www.codilar.com/
 */

class Person
{
    public function __call($name, $arguments)
    {
        echo "Sorry but method $name does not exist!";
    }
}

Now if we execute $person->getName(), it will say Sorry but method $name does not exist!, but not throw any errors. We can modify the class a little more

<?php
/**
 *
 * @package magento2
 * @author Codilar Technologies
 * @license https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
 * @link https://www.codilar.com/
 */

class Person
{
    private $name;
    
    public function __call($name, $arguments)
    {
        $type = substr($name, 0, 3);
        $key = strtolower(substr($name, 3));
        
        if ($key === "name") {
            switch ($type) {
                case "set":
                    $this->name = $arguments[0];
                    return $this;
                case "get":
                    return $this->name;
            }
        }
        return $this;
    }
}

And now we have a class where we can call $person->setName("Codilar")and $person->getName(); // returns "Codilar", even though both of these methods don’t actually exist in the Personclass. This same idea was used by Magento to come up with the \Magento\Framework\DataObjectclass.

That was all, for now. Hope you guys can come up with your own DataObjects now 😉 . Do let us know in the comment section below about what you want our next Magento tutorial to be about. See you till next time!

***

Our previous tutorials

What is x-magento-init?

What is KnockoutJS and how is it relevant in Magento 2?

Love videos? Watch our Magento 2 video tutorials on YouTube

Picture of Tamjeed Qazi

Tamjeed Qazi

Experienced IT professional with expertise in e-commerce development, performance commerce, and full SDLC project development. Skilled in business strategy planning, IT service management, IT governance, and compliance.

You May Also Like

Latest Blogs

Magento Development Company

Let’s talk

Our Offices

DTECH, Techno Hub 1, Dubai Silicon Oasis Authority, United Arab Emirates – Dubai – United Arab Emirates

Singapore

Codilar Digital Pte Ltd, 68 Circular Road, #02-01, 049422, Singapore

India

Bengaluru

7th Floor, Jupiter Block ,Prestige Tech Park, Kadubeesanahalli, Bellandur Amanikere, Bengaluru, Karnataka 560103

Calicut

SBC No 4 & 6 upper basement, Sahya Building
KSITIL SEZ, Cyberpark Kozhikode Park Rd, Nellikkode, Kozhikode, Kerala 673016

Kolkata

Astra Towers, ANO -523 ,North Block, Action Area IIC, Newtown, Kolkata, West Bengal 700135

Ahmedabad

Codilar Technologies, Connekt, 13th Floor, Gala Empire, Opposite T.V. Tower, Drive In Rd, Memnagar, Ahmedabad, Gujarat – 380052

Oman

Building No. 2/796, Way No. 43, Block No. 336, Al Khud 132, Muscat, Oman

Codilar

© Copyright Codilar 2025. All Rights Reserved. Privacy Policy

Send Feedback

Request PWA Demo