Blog background
May 9, 2019|Read • 3 Min

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

Written by
Tamjeed Qazi
Tamjeed Qazi
Data objects

Listen Full Blog Here

Last Updated: Jul 3, 2026

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

  • __construct() – The constructor to a class
  • __destruct() – The destructor to a class
  • __get() – Called when we try to access the value of a property that does not exist
  • __set() – Called when we try to set the value of a property that does not exist
  • __call() – Called when we try to access an inaccessible/non-existent method. (This is what we’re concerned with right now)

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!

***

Love videos? Watch our Magento 2 video tutorials on YouTube


Our previous tutorials


What is x-magento-init?


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

Related Blogs

Loading...

Our Offices Are Here

UAE flag

UAE

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

+971 55 557 8583

Saudi Arabia flag

Saudi Arabia

Level 1, Building 7, Zone A Airport road, Business Gate P.O Box 93597 Riyadh 11683, KSA.

+966 50 809 6356

Oman flag

Oman

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

+968 7694 6200

Singapore flag

Singapore

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

India flag

India

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

+91 888 49 00 505

Indonesia flag

Indonesia

Satrio Tower, Floor 6, Unit C and D, Desa/Kelurahan Kuningan Timur, Kec. Setiabudi, Kota Adm. Jakarta Selatan, Provinsi DKI Jakarta