Magento shell command fatal error: Class "Mage" not found

magento, magento-1.7, php, php-5.3

Solution

You've extended the base abstract class incorrectly. If you look at the abstract constructor

#File: shell/abstract.php
public function __construct()
{
    if ($this->_includeMage) {
        require_once $this->_getRootPath() . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
        Mage::app($this->_appCode, $this->_appType);
    }

    $this->_applyPhpVariables();
    $this->_parseArgs();
    $this->_construct();
    $this->_validate();
    $this->_showHelp();
}

You can see this is where the shell scripts `require` in the `Mage` application class. Your class has redefined the constructor using the older PHP syntax

public function Mage_Shell_RegularPromos($promoID, $day)
{
    $this->day = $day;
    $this->promoID = $promoID;
    $this->rule = Mage::getModel('salesrule/rule');
}

but you've not called the parent constructor. This means the mage class isn't `requir`ed, and other important initialization doesn't happen. I'd redefine your constructor method such that it uses the newer `__construct` syntax, and call the parent constructor

public function __construct($promoID, $day)
{
    parent::__construct();
    $this->day = $day;
    $this->promoID = $promoID;
    $this->rule = Mage::getModel('salesrule/rule');
}

Problem

When I run this command from within the /shell/ directory: ``` php -f regularPromos.php ``` Why do I receive this error? ``` PHP Fatal error: Class 'Mage' not found in /var/www/vhosts/yoohoo/httpdocs/shell/regularPromos.php on line 28 ``` This is regularPromos.php: ``` <?php require_once 'abstract.php'; class Mage_Shell_RegularPromos extends Mage_Shell_Abstract { //Day of week to repeat promotion protected $day; //ID of promotion protected $promoID; //Rule process object protected $rule; public function Mage_Shell_RegularPromos($promoID, $day) { $this->day = $day; $this->promoID = $promoID; $this->rule = Mage::getModel('salesrule/rule'); } public function run() { date_default_timezone_set('America/New_York'); $nextWeek = date('Y-m-d', strtotime('next '. $this->day)); $rule = $rule->load($this->promoID); $rule->setFromDate($nextWeek) ->setToDate($nextWeek) ->save(); } } $shell = new Mage_Shell_RegularPromos(7, 'monday'); $shell->run(); ?> ``` Per all the SO threads I could find on this issue: I've tried running with compiler on/off/cleared/compiled, with the same error message I've cleared cache via the admin panel and manually deleting everything in /var/cache/. APC does not show up in phpinfo(), so that shouldn't be an issue either. I can run compiler.php just fine, so I assume I've just made a mistake in my php above. I'm running Magento 1.7 CE, PHP 5.3.3

Original source