PHP object-oriented command mode, php object-oriented command
This mode consists of the command class, user request data class, business logic class, command class factory class, and call 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: obtain user request data and save the results returned after background processing;
3. Business logic: for example, the following example verifies whether the user login information is correct;
4. Command factory class (my own name, haha): generate an instance of the command class. This class was a bit embarrassing when I first looked at it, of course, after reading it several times, I still think it is awesome :);
5. Call class: Call the command class to generate a view;
View the Code directly:
// Command class abstract class Command {abstract function execute (CommandContext $ context);} class LoginCommand extends Command {// function execute (CommandCotext $ context) Command class for processing user login information) {// CommandCotext is a class for processing user request data and background feedback data $ manager = Registry: getAccessManager (); // no specific implementation is found in the original code, it indicates that this is a business logic class for processing user login information $ 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; // return true if the user logs in successfully} 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 ;}} // user request data 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 factory, this class generates the command class CommandNotFoundException extends Exception {} class CommandFactory {private static $ dir = 'commands' based on the action in the user request data '; 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 "; // DIRE CTORY_SEPARATOR represents '/', which is the path of a command class file if (! File_exists ($ file) {throw new CommandNotFoundException ("cocould not find '$ file'");} require_once ($ file); if (! Class_exists ($ class) {throw new CommandNotFoundException ("no' $ class' class located") ;}$ cmd = new $ class (); return $ cmd ;}} // The 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'); // use the command Factory Class to obtain the command class if (! $ Comd-> execute ($ this-> context )) {// processing Failed} else {// success // distribution view} // client $ controller = new Controller (); // counterfeit user requests, in real scenarios, these parameters should be obtained through post or get. It seems nonsense: $ context = $ controller-> getContext (); $ context-> addParam ('action', 'login'); $ context-> addParam ('username', 'bob'); $ context-> addParam ('pass ', 'tiddles '); $ controller-> process ();