Categories


Archives


Recent Posts


Categories


Magento 2: Magic Getters and Setters

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Magento’s objects (that inherit from Varien_Object) have special “magic” getter and setter methods implemented with PHP’s magic __call method. If you’ve got a Magento object, you can just set and get data like this

$this->setFooBar('a value');
echo $this->getFooBar();

without any setFooBar or getFooBar methods defined on your objects.

There’s been a bit of conventional wisdom floating around since 2011 that these getters and setters are going away in Magento 2. However, as of the latest preview release, they’re still in place.

Removing these magic methods would mean a lot of refactoring for extension developers, but these same magic methods are often a key contributor to the confusion a new developer has with Magento. My guess is they stay in, although with no official release date for Magento 2, anything can happen.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 23rd September 2013