1

I am using Magento 1.7.

In recent.phtml under app/design/frontend/default/custom_theme/template/sales/order, there is the following call:

$_orders = $this->getOrders();

and later on $_orders is used to extract each one of the orders. I know that getOrders() returns the orders for that customer, but I was trying to find the definition of getOrders(), but seem to be not able to find it.

I used <?php var_dump(get_class($this)); ?> at the start of recent.phtml to look for the method in the returned class, but still can't find it.

I also read that from the use of Zend, it is possible to use set or get functions that are not defined.

Could you please tell me if I understood something wrong and how could I find the method definition? Any help will be much appreciated.

2 Answers 2

2

set and get is magento's magic methods

Magento magic methods are define in class Varien_Object located in lib/Varien/Object.php

    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).")");
    }

What Magento (or PHP for that matter) does is looks for a method called getOrders() . Simple enough. When it cannot find it, it looks up through it’s parent classes until it simply cannot find getOrders() anywhere. When it reaches this point, it goes through it’s last resort, the __call() method. Basically it passes the method and it’s arguments to the __call() method.

What Magento does from this point is sees whether or not the method begins with any of these prefixes: get, set, uns, has. From there, it accesses or manipulates the protected $_data array within the object.

get fetches the data, set sets new data in the key, uns unsets data from that key, and has returns a boolean value on whether or not that key exists in the array.

So magento Mage_Sales_Block_Order_Recent::__construct() method setOrders order using $this->setOrders($orders); and we can fetch that data using magento magic methos $this->getOrders();

To know more about magic mathod visit this reference link

0
3

Open this file app\code\core\Mage\Sales\Block\Order\Recent.php. Check inside the __construct they are setting the orders.

like this $this->setOrders($orders);.

3
  • I see that, but I don't see a getOrders() method definition, why is that?
    – LeCodeur
    May 12, 2017 at 12:48
  • Its not a method. Its using php's setter and getter. You can find it inside lib\Varien\Object.php. Also please accept the answer if it helps. May 12, 2017 at 12:54
  • Thanks, that was what I was asking, could you add it to your answer so that I can accept it
    – LeCodeur
    May 12, 2017 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.