Php 中间壳层
我有一个CakePHP项目,其中包含三个shell脚本,它们将作为cron作业运行。这些cron作业的输出将有助于调试。我们已决定将此输出通过电子邮件发送给支持团队。我的计划是创建一个类Php 中间壳层,php,cakephp,inheritance,autoload,Php,Cakephp,Inheritance,Autoload,我有一个CakePHP项目,其中包含三个shell脚本,它们将作为cron作业运行。这些cron作业的输出将有助于调试。我们已决定将此输出通过电子邮件发送给支持团队。我的计划是创建一个类CronShell,该类扩展AppShell,然后为将作为cron作业运行的任何脚本扩展CronShell。下面是CronShell类: class CronShell extends AppShell { private $_messages = array(); public functio
CronShell
,该类扩展AppShell
,然后为将作为cron作业运行的任何脚本扩展CronShell
。下面是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');
}
}
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');
}
}
但结果是:
PHP Fatal error: Class 'CronShell' not found in...
如何让
TestShell
意识到CronShell
?感谢Abdou Tahiri和Kai对缺失内容的解释。我只需要在继承自CronShell
的脚本中使用App::uses
调用。下面是正在运行的TestShell
:
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');
}
}
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');
}
}
这只是一个名称空间问题,我想我以前没见过有人使用shell脚本,所以我不确定如何使用,但我会尝试
App::uses('CronShell','Console/Command')
谢谢@Kai,@AbdouTahiri我不知道App::uses的第二个参数是目录。阅读文档有助于+1.请注意,使用特征是另一个类似的选择,或者将功能放在任务中。您只能像这样使用一次继承,而您可以使用任意数量的任务(或特征)。