PHP single-piece mode combined with command chain mode instructions

Source: Internet
Author: User

For some people, Article The content is too simple. This is a tutorial for beginners, because the time is relatively tight (to accompany his wife to go shopping, huh, huh), where the design is not standardized, Code The writing is not standard, and the bug is also pointed out by various heroes to facilitate common progress. My level is limited. ^_^

I believe that everyone has read many books or articles about the application of design patterns in PHP, but few of them directly give examples. Most of them are confused after reading them. If there is no project practice, it is difficult to clarify the design pattern.

To avoid Code complexity, no exception handling is added.
The basic knowledge of the single-piece mode and the command chain mode is as follows: Copy code The Code is as follows: <? PHP
/*
* @ Author: noangels
* @ Time: January August 30
*/
Interface irunaction {
// Obtain the methods defined in the class that can be run in the app
Static function loadactions ();
// The entry function in the class calls other functions in the class
Function runaction ($ action, $ ARGs );
}
/*
* Core Components of APP Systems
*/
Class app {
Static private $ __instance = NULL;
Static private $ __commands = array ();
Static private $ __flag = 1;
Private function _ construct (){}
// Design the unique instance of the class in single-piece Mode
Static function load (){
If (Self ::$ __ instance = NULL) Self ::$ instance = new app;
Return self: :__ __ instance;
}
// Add the instance named $ __instance to the app. Check whether an instance of this class has been added before each time a new command is added.
// If yes, ignore the operation and add it if no.
Public Function addcommand ($ role name ){
Foreach (Self ::__ commands as $ cmd ){
If (strtolower (get_class ($ cmd) = strtolower (get_class ($ variable name ))){
SELF: $ __flag = 0;
Break;
}
}
If (Self ::$ __flag = 1) Self ::__ __commands [] = $ alias name;
SELF: $ __flag = 1;
}
// The entry function of the instance is called in the core part of the command chain mode design.
// First, check whether the operation is allowed to be called in the class. If not, an undefined operation is prompted to exit.
Public Function runcommand ($ action, $ ARGs ){
SELF: $ __flag = 0;
Foreach (Self ::__ commands as $ cmd ){
If (in_array ($ action, $ cmd-> loadactions ())){
SELF: $ __flag = 1;
$ Cmd-> runaction ($ action, $ ARGs );
}
}
If (Self ::$ __flag = 0 ){
SELF: $ __flag = 1;
Exit ("undefined action by Action: $ action ");
}
}
// Delete an instance of a class. You only need to specify the class name.
Public Function removecommand ($ classname ){
Foreach (Self ::__ commands as $ key => $ cmd ){
If (strtolower (get_class ($ cmd) = strtolower ($ classname )){
Unset (Self ::__ __commands [$ key]);
}
}
}
// You can test whether or not the deletion is successful.
Public Function viewcommands (){
Echo (count (Self ::__ commands ));
}
}
// Class user implementation interface irunaction
Class user implements irunaction {
// Define the operation that can be called
Static private $ __actions = array ('adduser', 'modifyuser', 'removeuser ');
// Obtain the operation that can be called. Do not directly love you in the actual process. $ __actions is designed as a public call.
// Instead, design a loadactions function to get the value of $ __actions.
Static public function loadactions (){
Return self: :__ __ actions;
}
// Run the specified function
Public Function runaction ($ action, $ ARGs ){
// If you do not understand the usage of this function, refer to the manual.
Call_user_func (Array ($ this, $ action), $ ARGs );
}
// Test the function.
Protected function adduser ($ name ){
Echo ($ name );
}
}
// Similar user of the test class
Class test implements irunaction {
Static private $ __actions = array ('addtest', 'modifytest', 'removetest ');
Static public function loadactions (){
Return self: :__ __ actions;
}
Public Function runaction ($ action, $ ARGs ){
Call_user_func (Array ($ this, $ action), $ ARGs );
}
Protected function addtest ($ name ){
Echo ($ name );
}
}
// The following is the test code.
APP: load ()-> addcommand (New User );
APP: load ()-> addcommand (New User );
APP: load ()-> addcommand (New User );
APP: load ()-> addcommand (New User );
APP: load ()-> runcommand ('adduser', 'noangels ');
APP: load ()-> addcommand (new test );
APP: load ()-> runcommand ('addtest', null );

The app class is designed in single-piece mode and is the core part of the system. I believe you can see from the code that the load method loads app-class instances, which is equivalent to the getinstance static method in some books. he has three public methods: addcommand, runcommand, and removecommand. runcommand is the core part. it is also the core startup of the command chain mode.ProgramFor more information, seeSource codeThe code is clearly written, so I will not repeat it here.
The class user and test implement the interface irunaction. Both Classes define a static private variable $ __actions, which is an array and contains operations that can be called by the runcommand function of the app.

The following is the system running process:

App startup
------- Addcommand: adds the class of the operation to be executed to the app. if the added class is designed in single-piece mode. you can add addcommand (singletonclass: load () as follows ()). otherwise, you can call

Addcommand (New someclass)
------- Runcommand. operation. for example, there is an operation adduser in the user class. I can directly enable runcommand ($ acttion, $ ARGs ). loop through the $ __commands array in the app. If an instance of a class has this operation, the runaction function of the instance is called. if you have not added an instance of a class using addcommand, an undefined operation is prompted to exit.
Runaction in class user and class test calls call_user_func, a very useful function. Calls the corresponding function in this class.

Tip: the explanations and examples are here. How do you understand and use this idea depends on your understanding. You must do everything yourself. (PS: it can be made into a single entry file in the framework. If MVC is not implemented, it depends on what you think .)

the actual running effect is as follows:
if you do not understand the language level, contact me.
I will write some articles for you later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.