2

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

2
  • Should this help?
    – karthikr
    Commented Aug 19, 2013 at 16:31
  • No, because "include_once "Maged/Controller.php";" is not in that file on my system. I also don't think this has anything to do with the downloader, which just seems to be for updating Magento, correct? Commented Aug 19, 2013 at 16:34

4 Answers 4

8

Maybe if you add this in the begin of you script:

require_once('./app/Mage.php');
Mage::app();

Greetings

4
  • Strange, abstract.php should already load Mage.php. Adding those two lines above require_once('abstract.php') changed the error to "PHP Fatal error: Call to a member function load() on a non-object ... on line 38)" Commented Aug 19, 2013 at 16:40
  • Try with:$this->rule->load($this->promoID); Commented Aug 19, 2013 at 17:58
  • You are correct, sir! Now why did I need to require Mage.php when I already require abstract.php, which loads Mage.php in its constructor? Commented Aug 19, 2013 at 18:09
  • 1
    @werdnanoslen See below for your Mage.php problem. Commented Aug 19, 2013 at 18:13
5

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 required, 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');
}
4
  • +1, but won't this break signature compatibility with the parent __construct? A bit more refactoring to put the promo id and day in $data would be more idiomatic, no?
    – kojiro
    Commented Aug 19, 2013 at 18:20
  • @kojiro I think this would still pass PHP's strict standards as the parent has no arguments, but you're 100% correct in that it'd be best to get the $promoId and $day variables into this object in another way. However, these are the scripts in /shell/ so there's no $data array. Thinking about it briefly, "the right" thing to do would be make these command line arguments and set things up in _construct, although ultimately I think "the right" thing is the thing a programmer's willing to support. Commented Aug 19, 2013 at 18:40
  • @AlanStorm Yes! That's the sort of thing I wasn't understanding, and it works as I expect it to. I'll look up the difference between the two constructor syntaxes. Commented Aug 19, 2013 at 18:41
  • I'll probably make this more generalized and release it as such later, but for now I'm just using it for two weekly promotions (next friday, next saturday) thus I will have two weekly cron jobs run this script with the necessary parameters. Commented Aug 19, 2013 at 18:46
1

I had same problem but with core shell scripts like indexer.php. I'm still not sure what caused the problem, but it have to be somehow environment related because shell scripts are working fine on development server, but caused problems on production server.

Basically I received this error:

PHP Fatal error:  Class 'Mage' not found in abstract.php on line 86

This problematic code is:

84. if ($this->_includeMage) {
85.    require_once $this->_getRootPath() . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
86.    Mage::app($this->_appCode, $this->_appType);
87. }

Then I changed require_once to require and poof error disappeared and everything works just fine. And it seems to work on dev server too.

-1

When this type of error occurs:

Fatal error: Class 'Mage' not found in /home1/sumit/public_html/index.php

Without refreshing the page, replace index.php to cache_flush then hit enter.

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.