Magento Interview Question Part-1

I have been interviewing in Magento from last three years and also given several ;). Below I am sharing my experience and also basic Magento question that interviewer frequently asked.

1. What is Magento?
Well nobody will ask you this silly question, but just to start let’s discuss this. It is an open source e-commerce platform, enriched with lots of functionality and it has a very strong catalog management system which is unbeatable by any other platform in a market.

2. What is code pool in Magento? How many types of they are?
Most basic which every Magento developer know, Magento has provided a different section to for writing up your module and provided different significance to them, basically, three code pools are there: core, local and community.
Below is the Purpose of different code pool:-
Core :- Magento core developer code is placed here. We never place our code in core code pool, neither make any changes over here.
Local :- Magento developer who make their own module or customized Magento default functionality place their code here.
Community :- When we download an extension from Magento community then we normally place extension here.

There is a sequence in which Magento calls these code pool. Suppose the same file is overridden in local and community then local is always given preference. As it can be seen in Mage.php line 43

    $paths = array();
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
    $paths[] = BP . DS . 'lib';

Above code clearly tells that firstly local, then community, then core code pool will be executed. Thus when we override our file we place them mostly under local code pool.

3. What are routers in Magento and tell the sequence in which they are called?
Magento uses a front controller which instantiate router object and router object check the request URL for the match with controller and action. Thus routers are responsible for routing a request in Magento. Magento uses four type of router and they are called in below sequence:-
Mage_Core_Controller_Varien_Router_Admin
Mage_Core_Controller_Varien_Router_Standard
Mage_Cms_Controller_Router
Mage_Core_Controller_Varien_Router_Default

Code can be seen in Mage_Core_Controller_Varien_Front class on line 169 in dispatch() function

while (!$request->isDispatched() && $i++ < 100) { foreach ($this->_routers as $router) {
                /** @var $router Mage_Core_Controller_Varien_Router_Abstract */
                if ($router->match($request)) {
                    break;
                }
            }
        }

A loop is executed maximum 100 times for each request if it does not fall under any router and request are matched will all router sequentially.

4. How can we enhance the Magento performance?
This question has vast options to answer with, your practical knowledge can provide a better answer but below are some steps that we do take before moving our web application to a production environment to speed up Magento.
Disable the Magento log (Writing up log might take some time thus disabling then helps in speeding up but you will then not be able to find out the reason of certain issue. In that case forced log option helps)
Disable any unused modules(we should take care not to enable any unused module as it will then be an overhead on Magento because config of that module will always be taken care of)
Magento Caching
Enable Gzip compression
Optimize your image
Optimize your Server
Use a Content Delivery Network (CDN)
USE Gzip Components
Put Stylesheets at the Top (CSS Files in head tag). Also, there is an option to merge all your stylesheet and JS file into single file to reduce HTTP request.
Put Scripts at the Bottom (JS files in footer)
Avoid CSS Expressions (100/2) Minor but can be taken care of.

5. What technology does Magento use?
This could be the silliest question that can be asked from a fresher. Well, Magento uses PHP for programming and MYSQL for database.

6. List different design pattern that Magento use?
This is a very known question for Magento people. Magento follows several types of design pattern such as:-
Singleton Pattern
Factory Pattern
Front Controller Pattern
Registry Pattern
Event Observer Pattern
Prototype Pattern
Object Pool Pattern
Iterator Pattern
Lazy Loading Pattern
Module pattern

7. Describe Singleton and factory pattern. what is the difference between them and how does Magento uses them?
The Singleton pattern is responsible for instantiating a class but it first verifies that if an object of that class is already instantiated or not, if yes then it will return an already created object. On the other side, Factory pattern is also responsible for factorizing (instantiating) classes. It’s widely used throughout the Magento and leverages the autoloading system in Magento. By defining an alias in module config.xml file we are letting the factory know where it can find classes.
For using singeleton pattern we uses Mage::getSingleton(“class alias”); mainly we use it for customer/session, core/session
For using factory pattern we have Mage::getModel(“class alias”);

getSingleton and getModel function definition is found in Mage.php

8. what is the difference between using $this->__(“any text”) and Mage::helper(‘catalog’)->(“any text)?
Well, both works similarly and get the text from a language file. But Mage::helper(‘catalog’)->(“any text”) make sure that specific helper will be used for translation. For example,
Suppose we have a class Mage_Adminhtml_Block_Catalog_Product_Grid_Block
Now, for column header we have,
“header” => Mage::helper(“catalog”->__(“Name”)
So if we will not specify catalog here then adminhtml helper will be considered.

Also, suppose we override some file then also issue can come. For example we have class Mage_Catalog_Block_Breadcrumbs and there is code Mage::helper(“catalog”)->__(“Home”)
Now we override namespace_Modulename_Block_Breadcrumb
if $this->__(“Home”) was used then “Modulename” was used.

9. What is lazy loading?
In Magento Lazy loading is mainly used in ORM or in DATA Mapper, as the name suggest it delays loading of an object until object is called upon. In magento lazy loading is mainly utilized with data not objects.
For example, we heavily uses collection in Magento and we have below code:-

       $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->addStoreFilter($this->getRequest()->getParam('store'))
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.(int) $this->getRequest()->getParam('id', 0),
                'left');
        $this->setCollection($collection);

Above code is just setting or making a query for the database but it is not actually interacting with DB until we actually want to get the data. So if we use $collection->getData() then the collection will interact with database, so it has delays the loading of data.

10. What are getter/setter methods and how does Magento uses them?
PHP has certain magic methods which are called automatically according to their implementation. For example,
__call() :- This method is called automatically when PHP cannot find a method which is called somewhere.

In lib/varien/object.php we have defined this method

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' :
                /* Some code here */
            case 'uns' :
                /* Some code here */

            case 'has' :
                /* Some code here*/
        }
        throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
    }

So above you can see case that checks for ‘get’, ‘set’, ‘uns’ and ‘has’. These methods are called whenever Magento code calls any method which is not defined anywhere throughout the code. Now we knows that to fetch value of any field we use getdata(‘fieldname’) function and we also have another way getFieldName()
Now GetData() is a function that is found in lib/varien/object.php and will thus return us with required value but getFieldName() is not defined anywhere so __call will be called and as it can be seen above it will then get the method name and execute as per required functionality.

Leave a Comment.