PHP面向对象中的命令模式的构成以及作用
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容介绍了"PHP面向对象中的命令模式的构成以及作用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2026年11月02日PHP面向对象中的命令模式的构成以及作用
本篇内容介绍了"PHP面向对象中的命令模式的构成以及作用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
这个模式主要由 命令类、用户请求数据类、业务逻辑类、命令类工厂类及调用类构成,各个类的作用概括如下:
1、命令类:调用用户请求数据类和业务逻辑类;
2、用户请求数据类:获取用户请求数据及保存后台处理后返回的结果;
3、业务逻辑类:如以下的示例中验证用户登陆信息是否正确的功能等;
4、命令工厂类(我自己取的名字,哈哈):生成命令类的实例,这个类第一次看的时候我觉得有点屌,当然看了几遍了还是觉得很屌 :);
5、调用类:调用命令类,生成视图;
直接看代码:
//命令类abstract class Command { abstract function execute(CommandContext $context);}class LoginCommand extends Command{ //处理用户登陆信息的命令类 function execute (CommandCotext $context){ //CommandCotext 是一个处理用户请求数据和后台回馈数据的类 $manager = Registry::getAccessManager(); //原文代码中并没有具体的实现,但说明了这是一个处理用户登陆信息的业务逻辑类 $user = $context->get('username'); $pass = $context->get('pass'); $user_obj = $manager->login($user,$pass); if(is_null($user_obj)){ $context->setError($manager->getError); return false; } $context->addParam('user',$user_obj); return true; //用户登陆成功返回true }}class FeedbackCommand extends Command{ //发送邮件的命令类 function execute(CommandContext $context){ $msgSystem = Registry::getMessageSystem(); $email = $context->get('email'); $msg = $context->get('msg'); $topic = $context->get('topci'); $result = $msgSystem->send($email,$msg,$topic); if(!$result){ $context->setError($msgSystem->getError()); return false; } return true; }}//用户请求数据类 class CommandContext { private $params = array(); private $error = ''; function __construct (){ $this->params = $_REQUEST;}function addParam($key,$val){ $this->params[$key] = $val;}function get($key){ return $this->params[$key];}function setError($error){ $this->error = $error;}function getError(){ return $this->error;}}//命令类工厂,这个类根据用户请求数据中的action来生成命令类class CommandNotFoundException extends Exception {}class CommandFactory { private static $dir = 'commands'; static function getCommand($action='Default'){ if(preg_match('/\w',$action)){ throw new Exception("illegal characters in action"); } $class = UCFirst(strtolower($action))."Command"; $file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php"; //DIRECTORY_SEPARATOR代表'/',这是一个命令类文件的路径 if(!file_exists($file)){ throw new CommandNotFoundException("could not find '$file'"); } require_once($file); if(!class_exists($class)){ throw new CommandNotFoundException("no '$class' class located"); } $cmd = new $class(); return $cmd; }}//调用者类,相当于一个司令部它统筹所有的资源class Controller{ private $context; function __construct(){ $this->context = new CommandContext(); //用户请求数据 } function getContext(){ return $this->context; } function process(){ $cmd = CommandFactory::getCommand($this->context->get('action')); //通过命令工厂类来获取命令类 if(!$comd->execute($this->context)){ //处理失败 } else { //成功 // 分发视图 } }}// 客户端$controller = new Controller();//伪造用户请求,真实的场景中这些参数应该是通过post或get的方式获取的,貌似又废话了:)$context = $controller->getContext();$context->addParam('action','login');$context->addParam('username','bob');$context->addParam('pass','tiddles');$controller->process();"PHP面向对象中的命令模式的构成以及作用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
命令
用户
数据
处理
业务
工厂
逻辑
登陆
作用
模式
信息
对象
成功
代码
内容
后台
更多
知识
视图
这是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技
互联网科技媒体
小马过河互联网科技
左右互联网络科技
富国互联网科技创新基金
互联网科技实习生岗位职责
福州新华互联网科技学校 知乎
成都铂石互联网科技做什么的
昭通互联网科技选哪家
速维有品互联网信息科技(苏州)有限公司
呼和浩特新华互联网科技学院
保险科技服务互联网营销体系
滨湖区互联网智能科技产品有哪些
2021互联网科技公司排名
广州富喜互联网络科技
互联网硬科技公司
兰石爱特工业互联网科技分公司
杭州融税互联网科技有限公司
互联网领先科技成果宣传视频
互联网科技创新方面的资料
互联网科技保险
狮子座互联网科技
杭州互联网科技公司
奇士达互联网科技
伴你行互联网科技有限公司
火传媒上海互联网科技
中正互联网科技招聘
广州新华互联网科技学校电竞方面
保险科技服务互联网营销体系
广州互联网高科技取餐柜