PHP aspect-oriented programming component _ PHP Tutorial

Source: Internet
Author: User
Tags autoload autoloader
PHP aspect-oriented programming component. We use the MVC framework, such as CI, YII, and cakePHP. one of the reasons is that the code can be easily maintained. However, when the business logic is constantly complicated, the more we call the methods in the model in the controller, the more we use the MVC framework, such as CI, YII, and cakePHP. one of the reasons is that the code can be easily maintained.

However, when the business logic is constantly complicated, the method in calling the model in the controller will become increasingly bloated.

The idea of aspect-oriented programming is one of the solutions to the ever-changing business logic and ease of code maintenance.


The following is the source code of the aspect component, which is designed based on the idea of AOP.

If (function_exists ('_ autoload ')){
Trigger_error ("Extension: It looks like your code is using an _ autoload () function. extension uses spl_autoload_register () which will bypass your _ autoload () function and may break autoloading. ", E_USER_WARNING );
}

Spl_autoload_register (array ('extensionfactory ', 'autoload '));

Class ExtensionFactory {

Private static $ extFamily = null;
Private static $ _ classes = array (
'Extension' => '/Extension. php ',
'Extensionfamily '=>'/ExtensionFamily. php'
);

/**
* Class autoloader. This method is provided to be invoked within
* _ Autoload () magic method.
* @ Param string $ className The name of the class to load.
*/
Public static function autoload (){
Foreach (self ::$ _ classes as $ v ){
Require_once dirname (_ FILE _). $ v;
}
}

/**
* Before you can call addExtension \ removeExtension, you must call this method to instantiate the extension family.
* @ Return ExtensionFamily
*/
Public static function createExtension (){
Self: $ extFamily = new ExtensionFamily ();
Return self: $ extFamily;
}

Public static function removeExtension ($ extName ){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Unset (self ::$ extFamily-> _ extensionArray [$ extName]);
}
}

Public static function addExtension ($ extName, Extension $ ext ){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Self: $ extFamily-> _ extensionArray [$ extName] = $ ext;
}
}

Public static function removeAllExtension (){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Foreach (self ::$ extFamily-> _ extensionArray as $ extName => $ ext ){
Unset (self ::$ extFamily-> _ extensionArray [$ extName]);
}
}
}
}
If (function_exists ('_ autoload ')){
Trigger_error ("Extension: It looks like your code is using an _ autoload () function. extension uses spl_autoload_register () which will bypass your _ autoload () function and may break autoloading. ", E_USER_WARNING );
}

Spl_autoload_register (array ('extensionfactory ', 'autoload '));

Class ExtensionFactory {

Private static $ extFamily = null;
Private static $ _ classes = array (
'Extension' => '/Extension. php ',
'Extensionfamily '=>'/ExtensionFamily. php'
);

/**
* Class autoloader. This method is provided to be invoked within
* _ Autoload () magic method.
* @ Param string $ className The name of the class to load.
*/
Public static function autoload (){
Foreach (self ::$ _ classes as $ v ){
Require_once dirname (_ FILE _). $ v;
}
}

/**
* Before you can call addExtension \ removeExtension, you must call this method to instantiate the extension family.
* @ Return ExtensionFamily
*/
Public static function createExtension (){
Self: $ extFamily = new ExtensionFamily ();
Return self: $ extFamily;
}

Public static function removeExtension ($ extName ){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Unset (self ::$ extFamily-> _ extensionArray [$ extName]);
}
}

Public static function addExtension ($ extName, Extension $ ext ){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Self: $ extFamily-> _ extensionArray [$ extName] = $ ext;
}
}

Public static function removeAllExtension (){
If (is_null (self ::$ extFamily )){
Throw new Exception ("Please createExtension first ");
Return false;
} Else {
Foreach (self ::$ extFamily-> _ extensionArray as $ extName => $ ext ){
Unset (self ::$ extFamily-> _ extensionArray [$ extName]);
}
}
}
}


/**
* Extended family
*
* @ Author Mr. Jin
*/
Class ExtensionFamily implements Extension {
Public $ _ extensionArray = array ();

/**
*
* @ Param type $ extName extension
* @ Param Extension $ ext: the object for implementing Extension
*/
Public function addExtension ($ extName, Extension $ ext ){
$ This-> _ extensionArray [$ extName] = $ ext;
}

Public function beforeAppend (& $ params ){
Foreach ($ this-> _ extensionArray as $ ext ){
$ Ext-> beforeAppend ($ params );
}
}

Public function afterAppend (& $ params ){
Foreach ($ this-> _ extensionArray as $ ext ){
$ Ext-> afterAppend ($ params );
}
}
}

?>
/**
* Extended family
*
* @ Author Mr. Jin
*/
Class ExtensionFamily implements Extension {
Public $ _ extensionArray = array ();

/**
*
* @ Param type $ extName extension
* @ Param Extension $ ext: the object for implementing Extension
*/
Public function addExtension ($ extName, Extension $ ext ){
$ This-> _ extensionArray [$ extName] = $ ext;
}

Public function beforeAppend (& $ params ){
Foreach ($ this-> _ extensionArray as $ ext ){
$ Ext-> beforeAppend ($ params );
}
}

Public function afterAppend (& $ params ){
Foreach ($ this-> _ extensionArray as $ ext ){
$ Ext-> afterAppend ($ params );
}
}
}

?>

/**
* Extended interfaces
*
* @ Author Mr. Jin
*/
Interface Extension {
Public function beforeAppend (& $ params );

Public function afterAppend (& $ params );
}

?>
/**
* Extended interfaces
*
* @ Author Mr. Jin
*/
Interface Extension {
Public function beforeAppend (& $ params );

Public function afterAppend (& $ params );
}

?>

The above three files implement simple AOP components.

The following is a Demo:

/**
* Custom Extension
* User credit Extension
* Determines whether to record user points for this consumption based on whether the user logs on.
*
* @ Author Mr. Jin
*/
Class ExampleExtension implements Extension {
Public $ check = false;

Public function beforeAppend (& $ isLogin ){
If ($ isLogin ){
$ This-> check = true;
}
}

Public function afterAppend (& $ pointer ){
If ($ this-> check ){
// Add pointer
} Else {
Echo 'user not logged on, points not input ';
Return;
}
}

}

?>
/**
* Custom Extension
* User credit Extension
* Determines whether to record user points for this consumption based on whether the user logs on.
*
* @ Author Mr. Jin
*/
Class ExampleExtension implements Extension {
Public $ check = false;

Public function beforeAppend (& $ isLogin ){
If ($ isLogin ){
$ This-> check = true;
}
}

Public function afterAppend (& $ pointer ){
If ($ this-> check ){
// Add pointer
} Else {
Echo 'user not logged on, points not input ';
Return;
}
}

}

?>


Demo. php
Require_once ('extensionfactory. php'); // import the component itself

Require_once ('exampleextension. php'); // Import extension

$ Ext = ExtensionFactory: createExtension ();

ExtensionFactory: addExtension ('example ', new ExampleExtension (); // points input function

/*
* The Extension can be added based on the change in requirements.
* Eg.
* New requirement: New Member types are added. price discounts are offered based on different types.
* Implementation ideas:
* 1. create a card number factory
* 2. create SeniorMemberExtension and PuTongMeberExtension.
* 3. Factory method addExtension based on membership type
*/

$ IsLogin = false; // assume that the user has not logged on

$ Ext-> beforeAppend ($ isLogin );


/**
* For aspect-oriented programming, the most important point is that the focus of the entire business process must be analyzed first.
* The key here is the order storage.
* The business logic may continue to increase before the order is placed into the warehouse, such as logon verification and card balance verification.
* After the order is placed into the warehouse: point processing, order monitoring, etc.
*/
Echo "here is the main business logic: Order warehouse receiving \ r \ n ";

$ Pointer = 100;

$ Ext-> afterAppend ($ pointer );
Require_once ('extensionfactory. php'); // import the component itself

Require_once ('exampleextension. php'); // Import extension

$ Ext = ExtensionFactory: createExtension ();

ExtensionFactory: addExtension ('example ', new ExampleExtension (); // points input function

/*
* The Extension can be added based on the change in requirements.
* Eg.
* New requirement: New Member types are added. price discounts are offered based on different types.
* Implementation ideas:
* 1. create a card number factory
* 2. create SeniorMemberExtension and PuTongMeberExtension.
* 3. Factory method addExtension based on membership type
*/

$ IsLogin = false; // assume that the user has not logged on

$ Ext-> beforeAppend ($ isLogin );


/**
* For aspect-oriented programming, the most important point is that the focus of the entire business process must be analyzed first.
* The key here is the order storage.
* The business logic may continue to increase before the order is placed into the warehouse, such as logon verification and card balance verification.
* After the order is placed into the warehouse: point processing, order monitoring, etc.
*/
Echo "here is the main business logic: Order warehouse receiving \ r \ n ";

$ Pointer = 100;

$ Ext-> afterAppend ($ pointer );
Running result:
Here is the main business logic: Order warehouse receiving
Non-logged-on user, points not recorded

From God's blog

Bytes. However, when the business logic is constantly complicated, the more you call the method in the model in the controller...

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.