中级壳类

时间:2013-12-26 16:34:31

标签: php cakephp inheritance autoload

我有一个带有三个shell脚本的CakePHP项目,它们将作为cron作业运行。这些cron作业的输出对调试很有用。我们决定将此输出通过电子邮件发送给支持团队。我的计划是创建一个类CronShell,扩展AppShell然后扩展CronShell以用于将作为cron作业运行的任何脚本。这是CronShell类:

class CronShell extends AppShell {
    private $_messages = array();

    public function out($message=null, $newlines=1, $level=Shell::NORMAL) {
        parent::out($message, $newlines, $level);
        $this->_messages[] = $message;
    }   

    public function __destruct() {
        App::uses('CakeEmail', 'Network/Email');
        $email = new CakeEmail('default');
        $email->to(Configure::read('support.addresses'));
        $email->subject(get_class($this).' output');
        $email->send(implode("\n", $this->_messages));
    }
}

我有一个简单的测试实现,它继承自CronShell

class TestShell extends CronShell {

    public function startup() {
        // Omit the startup message.
        return;
    }   

    public function main() {
        $this->out('test');
        $this->out('out');
        $this->out('the');
        $this->out('class');
    }
}

但输出是:

PHP Fatal error:  Class 'CronShell' not found in...

如何让TestShell了解CronShell

1 个答案:

答案 0 :(得分:1)

感谢Abdou Tahiri和Kai解释遗漏的内容。我只需要从App::uses继承的脚本中进行CronShell调用。这是工作TestShell

App::uses('CronShell', 'Console/Command');

class TestShell extends CronShell {

    public function startup() {
        // Omit the startup message.
        return;
    }

    public function main() {
        $this->out('test');
        $this->out('out');
        $this->out('the');
        $this->out('class');
    }

}