1

Whilst working within Magento I have begun seeing a pattern throughout the core coding. However, it is something I have seen throughout many areas of PHP coding. When a variable is declared in a function, it can then be used. An example of this is within magento, whilst emulating the store there is this code:

    public function startEnvironmentEmulation(
        $storeId, 
        $area = Mage_Core_Model_App_Area::AREA_FRONTEND,
        $emulateSroreInlineTranslation = false
    ) {
        if (is_null($area)) {
            $area = Mage_Core_Model_App_Area::AREA_FRONTEND;
        }
           ...
           ...


You can see that $area is defined, and then re-defined if it is found to be null.

Is it important to re declare the same variable if it is found to be null, as a kind of re-try? Or am I missing something?

Any help in understanding this would be appreciated!

Richard Cripps
  • 193
  • 1
  • 11
  • 1
    Its due to an inadaquancy in phps handleing of default parameters. There is no way to set the 3rd parameter in this method without setting the 2nd one, which kind of cripples default parameters in php: http://stackoverflow.com/questions/9166914/php-using-default-arguments-in-a-function – Steve Oct 23 '14 at 13:49
  • Thank you for the additional question. I really struggled to find anything relevant! – Richard Cripps Oct 23 '14 at 13:53

1 Answers1

3

I think this is done because you can set the third parameter without change the default value of the second. Example:

startEnvironmentEmulation($storeId, null, true); 
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
  • 1
    Oh! That makes far more sense than just trying to fill a variable with a null value twice in a row! Thank you for the clarification. I'l accept when i can! – Richard Cripps Oct 23 '14 at 13:50