2

I want to make use of Magic Methods in my Custom Module.

I created a new Model with different member variables (variable1, variable2) and I want to get them like getVariable1.

How do I need to declare those member variables? Can I set them on private? What else am I missing?

Somewhere I read I need to save them in $_data['variable1'], but that doesn't work.

Thanks!

5

Magic methods are implemented via Varien_Object class.

public function __call($method, $args)
{
    switch (substr($method, 0, 3)) {
        case 'get' :
            //Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
            //Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
            return $data;

        case 'set' :
            //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
            //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
            return $result;

        case 'uns' :
            //Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $result = $this->unsetData($key);
            //Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
            return $result;

        case 'has' :
            //Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            //Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
            return isset($this->_data[$key]);
    }
    throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
}

You'll need to either extend that class or any other class which extends from Varien_Object like Mage_Core_Model_Abstract for example. You are not required to define any variables or methods in your model as long as your method starts with get, set, uns and has, that is assuming you want them to work as magic methods.

So you can write something like this:

$model = Mage::getModel('namespace_module/sample');
$model->setSomeData('Hello');
echo $model->getSomeData();

If you do Zend_Debug::dump($model); you'll notice that your data is stored in _data property which is declared in Varien_Object class.

Example:

object(Namespace_Module_Model_Sample)#128 (7) {
  ["_data":protected] => array(1) {
    ["some_data"] => string(5) "Hello"
  }
  ["_hasDataChanges":protected] => bool(true)
  ["_origData":protected] => NULL
  ["_idFieldName":protected] => NULL
  ["_isDeleted":protected] => bool(false)
  ["_oldFieldsMap":protected] => array(0) {
  }
  ["_syncFieldsMap":protected] => array(0) {
  }
}
  • Awesome, thanks a lot! – kilsen Jan 18 '15 at 1:25

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.