PHP Aspect-oriented programming component

Source: Internet
Author: User
Tags autoload

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.

<? Phpif (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 an * _ 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 ;}}/*** you must call this method to instantiate the extension family before calling addextension, removeextension, etc. * @ 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]) ;}}}

<? PHP/*** extension family ** @ author mr. jin */class extensionfamily implements extension {public $ _ extensionarray = array (); /***** @ Param type $ extname extension * @ Param extension $ Ext: the object that implements 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) ;}}?>

<? PHP/*** extension interface ** @ 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:

<? PHP/*** custom extension * user point 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 in, points not input '; return ;}}?>

Demo. php

<? Phprequire_once ('extensionfactory. PHP '); // import the component itself require_once ('exampleextension. PHP '); // import extension $ ext = extensionfactory: createextension (); extensionfactory: addextension ('example', new exampleextension ()); // point entry function/** you can add extension as required. * 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. The factory method is based on the member type addextension */$ islogin = false; // assume that the user has not logged on to $ ext-> beforeappend ($ islogin);/*** for Aspect-Oriented Programming, the most important thing is to analyze which is the focus of the entire business processing. * The key here is the order storage. * The business logic may continue to increase before the order is placed into the warehouse, for example, login verification, card balance verification, etc. * 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

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.