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

Data Objects

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

Author



Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Ecommerce roundup Oct

eCommerce Roundup October 2024

Halloween is just round the corner and businesses are creatively cashing in on the festive spirit with innovative strategies to

Join 40,000+ Magento pros who receive eCommerce insights, tips, and best practices.

Request PWA Demo