PHP命令行模式
<?php error_reporting(E_ALL); header('Content-Type:text/plain;charset=utf-8'); interface CommandApp { public function execute(); } abstract class CommandBase implements CommandApp { public $resource; public function setRsrcParams($key, $value) { $this->resource[$key] = $value; } public function getRsrcParams($key) { return isset($this->resource[$key]) ? $this->resource[$key] : array(); } public function setTplData($value) { $this->setRsrcParams('tplData', $value); } public function getTplData() { return $this->getRsrcParams('tplData'); } } class Invoker { private $cmdlist = array(); public function getCommandList() { return $this->cmdlist; } public function setCommand($cmd, $append = false) { $append !== true && $this->cmdlist = array(); if (!is_object($cmd)) { return; } $kls = get_class($cmd); if (isset($this->cmdlist[$kls])) { unset($this->cmdlist[$kls]); } $this->cmdlist[$kls] = $cmd; } public function setCommandList($cmdlist, $append = false) { if ($append !== true) { $this->cmdlist = array(); } if (is_array($cmdlist)) { foreach ($cmdlist as $cmd) { $this->setCommand($cmd, true); } } } public function clearCommandList() { foreach ($this->cmdlist as $kls => $cmd) { unset($this->cmdlist[$kls]); } } public function execute() { $res = array(); foreach ($this->cmdlist as $kls => $cmd) { $cmd->execute(); } return $res; } } class Cyber extends CommandBase { public function execute() { $tplData['cyber'] = '第一个命令'; $this->setTplData($tplData); } } class Imbar extends CommandBase { public function execute() { $tplData['imbar'] = '第二个命令'; $this->setTplData($tplData); } } class cia extends CommandBase { public function execute() { $invoker = new Invoker(); $cmd = new Cyber(); $commandlist[get_class($cmd)] = $cmd; $cmd = new Imbar(); $commandlist[get_class($cmd)] = $cmd; $invoker->setCommandList($commandlist); $invoker->execute(); $cia = array(); foreach ($invoker->getCommandList() as $cmd) { $cia = array_merge($cia, $cmd->getTplData()); } $invoker->clearCommandList(); $tplData['cia'] = $cia; $this->setTplData($tplData); } } $cia = new cia(); $cia->execute(); print_r($cia->getTplData()); ?>