In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "the composition and function of command mode in PHP object-oriented". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
This pattern is mainly composed of command class, user request data class, business logic class, command factory class and calling class. The functions of each class are summarized as follows:
1. Command class: call user request data class and business logic class
2. User request data class: get the user request data and save the results returned after background processing
3. Business logic class: such as the function to verify whether the user login information is correct in the following example
4. Command factory class (named by myself, ): generate an instance of the command class. I thought it was a bit cool when I saw it for the first time. Of course, I still thought it was cool after watching it several times:)
5. Call class: call the command class to generate a view
Look directly at the code:
/ / Command class abstract class Command {abstract function execute (CommandContext $context);} class LoginCommand extends Command {/ / Command class function execute (CommandCotext $context) {/ / CommandCotext is a class $manager = Registry::getAccessManager () that handles user request data and background feedback data. / / there is no specific implementation in the original code, but it shows that this is a business logic class $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; / / user successfully logged in and returned true}} class FeedbackCommand extends Command {/ / Command class 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;}} / / user request data class 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 }} / / Command class factory, this class generates the command class class CommandNotFoundException extends Exception {} class CommandFactory {private static $dir = 'commands'; static function getCommand ($action='Default') {if (preg_match (' /\ wicked force action)) {throw new Exception ("illegal characters in action");} $class = UCFirst (strtolower ($action)). "Command" based on the command class factory in the user's request data. $file = self::$dir.DIRECTORY_SEPARATOR. "{$class} .php"; / / DIRECTORY_SEPARATOR stands for'/', which is the path to a command class file 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;}} / / caller class, which is equivalent to a command that coordinates all resources class Controller {private $context; function _ construct () {$this- > context = new CommandContext () / / user request data} function getContext () {return $this- > context;} function process () {$cmd = CommandFactory::getCommand ($this- > context- > get ('action')) / / get the command class if (! $comd- > execute ($this- > context)) {/ / processing failed} else {/ / success / / Distribution View}} / / client $controller = new Controller () / / falsify user requests. In real scenarios, these parameters should be obtained by post or get, which seems to be nonsense again:) $context = $controller- > getContext (); $context- > addParam ('action','login'); $context- > addParam (' username','bob'); $context- > addParam ('pass','tiddles'); $controller- > process () This is the end of the content of "the composition and function of command mode in PHP object-oriented". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.